为行中的重复值创建条形图

时间:2015-11-26 11:12:50

标签: java swing

考虑我有下表,

    Slice Num               Quantity
       1                      100
       1                       50
       2                       20
       2                       100

我正在尝试使用表格中的详细信息创建条形图。当没有Slice编号的重复值时,它可以正常工作。我的要求是当两行的X轴值相同时我需要添加Y轴的值。这可能吗?

我收到以下错误:"无法计算不属于某个范围的类别的位置"。例外是:

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)

以下是代码的一部分。

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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

在条形图中要绘制重复值,在这种情况下,任何条形图在绘制前均会区别绘图值。因此解决方案是针对绘图值添加唯一的序列号,而值渲染则删除唯一值:

label
{
    customizeText:function(e)
    {
     return e.value; // remove unique value
    }
}