0

Consider I have the following table,

    Slice Num               Quantity
       1                      100
       1                       50
       2                       20
       2                       100

I am trying to create a bar chart with the details in a table. It works fine when there are no duplicate values of Slice number. My requirement is when the X axis value of two row is same I need to add the value of Y axis. Is that possible?

I am getting the following error : "Cannot compute position for a category that does not belong to a range". Exception is :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot compute position for a category that does not belong to a range
[2015/11/26-15:43:02.212]   at com.jidesoft.range.Category.position(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.d.a(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.d.b(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.d.a(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.d.computeAxisBreadth(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.Chart.update(Unknown Source)
[2015/11/26-15:43:02.212]   at com.jidesoft.chart.Chart.update(Unknown Source)

Here is the part of the code.

findFilteredRows(table);
String[] xAxisValues = getXAxisData(table, this.getColumnIndexXAxis());
double[] yAxisValues = getYAxisData(table, this.getColumnIndicesYAxis()[0]);

if (xAxisValues == null) {
    Log.error(ALGO_NAME, "Unable to format chart: x-axis not initialized", "formatSimpleChart");
    return;
} else if (yAxisValues == null) {
    Log.error(ALGO_NAME, "Unable to format chart: y-axis not initialized", "formatSimpleChart");
    return;
}
chart.setChartType(type);
DefaultChartModel cm = new DefaultChartModel();
CategoryRange<String> range = new CategoryRange<String>();

for (int i = 0; i < xAxisValues.length; i++) {
    if (yAxisValues[i] > this.getHighRange()) {
        this.setHighRange(yAxisValues[i]);
    }
    Highlight hl = new Highlight(xAxisValues[i]);
    ChartCategory<String> chartCat = new ChartCategory<String>(xAxisValues[i]);
    ChartPoint point = new ChartPoint(chartCat, yAxisValues[i]);
    point.setHighlight(hl);
    cm.addPoint(point);
    range.add(chartCat);
    if(chartType == ChartTypeSelection.BAR){
        chart.setHighlightStyle(hl,new ChartStyle(ChartBuilder.getBarColorFromScheme(i)).withBars());
    }else if (chartType == ChartTypeSelection.AREA){
        chart.setHighlightStyle(hl,new ChartStyle(ChartBuilder.getBarColorFromScheme(i)).withLineFill(lineColor));
    }
    else{
        chart.setHighlightStyle(hl,new ChartStyle(ChartBuilder.getBarColorFromScheme(i)));
    }
}
this.setHighRange(highRange + (highRange * (yAxisPadding / 100)));
chart.setXAxis(new CategoryAxis(range));
chart.setYAxis(new NumericAxis(new NumericRange(lowRange, highRange), yAxisLabel));
axisLabel.setText(this.xAxisLabel);
chart.setModel(cm, chartStyles[0]);




 private String[] getXAxisData(JTable table, int index) {
        try {
            Class dataClass = table.getColumnClass(index);
            Vector<String> xAxisVector = new Vector<String>();

            for (int i = 0; i < table.getRowCount(); i++) {
                String value = convertToString(dataClass, table.getValueAt(i, index));
                if (!isRowFiltered(i)) {
                    xAxisVector.add(value);
                }
            }
            return (String[]) xAxisVector.toArray(new String[xAxisVector.size()]);
        } catch (Exception e) {
            Log.error(ALGO_NAME, "Error formatting chart data:" + e.getMessage(), e);
            return null;
        }
    }

    private double[] getYAxisData(JTable table, int index) {
        try {
            Class dataClass = table.getColumnClass(index);
            Vector<Double> yAxisVector = new Vector<Double>();
            for (int i = 0; i < table.getRowCount(); i++) {
                Double value = convertToDouble(dataClass, table.getValueAt(i, index));
                if (!isRowFiltered(i)) {
                    yAxisVector.add(value);
                }
            }
            double[] returnVal = new double[yAxisVector.size()];
            int count = 0;
            for (Double d : yAxisVector) {
                returnVal[count++] = d.doubleValue();
            }
            return returnVal;
        } catch (Exception e) {
            Log.error(ALGO_NAME, "Error formatting chart data:" + e.getMessage(), e);
            return null;
        }
    }

    private void findFilteredRows(JTable table){
        int[] indices=new int[this.columnIndicesYAxis.length+1];
        for(int i=0;i<columnIndicesYAxis.length;i++){
            indices[i]=columnIndicesYAxis[i];
        }
        indices[indices.length-1]=this.columnIndexXAxis;

        filteredRows.clear();
        FilterableTableModel fm = (FilterableTableModel) table.getModel();
        for (int i = 0; i < table.getRowCount(); i++) {
            boolean rowFiltered=false;
            for(int j=0;j<table.getColumnCount();j++){
                Filter[] filters;
                filters = fm.getFilters(j);
                for (Filter filter : filters) {
                     if (filter.isValueFiltered(fm.getValueAt(i, j))) {
                         rowFiltered=true;
                         filteredRows.add(new Integer(i));
                         break;
                     }
                }
                if(rowFiltered) //if one cell filtered no need to go through the rest
                    break;
            }
        }
    }
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225

1 Answers1

0

In bar chart want duplicate value to draw, that case any bar chart distinct the plotting value before draw. So the solution is to add unique serial number against plotting value and while value render remove the unique value:

label
{
    customizeText:function(e)
    {
     return e.value; // remove unique value
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129