This document contains a list of the questions asked most often about JClass Chart. We at KL Group keep track of support questions in order to update this list.
JCChart c = new JCChart(JCChart.PLOT); FileDataSource f = new FileDataSource("source.dat"); c.getDataView(0).setDataSource(f);
c.getDataView(0).setChartType(JCChart.PIE);
JCTitle header = c.getHeader(); header.setIsShowing(true); header.setText("Top of the Chart", false); JCTitle footer = c.getFooter(); footer.setIsShowing(true); footer.setText("Footsies", false);
c.getLegend().setAnchor(JCLegend.SOUTHEAST);
JCChart c = new JCChart(JCChart.BAR); FileDataSource f = new FileDataSource("source.dat"); c.getDataView(0).setDataSource(f);
// Suppose there are 3 data series ChartDataView data = c.getDataView(0); data.getSeries(0).getStyle().getFillStyle().setColor(Color.black); data.getSeries(1).getStyle().getFillStyle().setColor(Color.green); data.getSeries(2).getStyle().getFillStyle().setColor(Color.blue);
// Get ChartStyle instance for data series JCChartStyle style = c.getDataView(0).getSeries(0).getStyle(); style.getFillStyle().setPattern(FillStyle.DIAG_HATCHED);
JCChart c = new JCChart(JCChart.PIE); FileDataSource f = new FileDataSource("source.dat"); c.getDataView(0).setDataSource(f);
JCPieChartFormat pie = c.getDataView(0).getPieChartFormat(); // Change other slice fill color JCFillStyle pieFillStyle = pie.getOtherStyle().getFillStyle(); pieFillStyle.setColor(Color.cyan); // All slices less than 10 percent of total will be placed in other pie.setThresholdMethod(JCPieChartFormat.SLICE_CUTOFF); pie.setThresholdValue(10.0);
// MyDataSource reads data from a file and places it in a // vector of vectors called dataStuff class MyDataSource { Vector dataStuff; }
Use the Chartable interface to make MyDataSource into a JClass Chart data source.
// To make MyDataSource work with JClass Chart, add Chartable class MyDataSource extends Chartable { Vector dataStuff; public int getDataInterpretation() { // Return format of the data return Chartable.ARRAY; } public java.lang.Object getDataItem(int row, int column) { if (dataStuff == null) return null; Object rval = null; try { rval = (Vector)(dataStuff.elementAt(row)).elementAt(column); } catch (Exception e) { rval = null; } return rval; } public java.util.Vector getRow(int row) { if (dataStuff == null) return null; java.util.Vector rval = null; try { rval = (Vector)(dataStuff.elementAt(row)); } catch (Exception e) { rval = null; } return rval; } public int getNumRows() { int rval = 0; try { rval = dataStuff.size(); } catch (Exception e) { return rval; } public String[] getPointLabels(int row) { // We don't care about point labels return null; } public String getSeriesName(int row) { // We don't care about series name return null; } public String getSeriesLabel(int row) { // Series label and name are the same return getSeriesName(row); } public String getName() { // Name of the data view return "MyData"; } }
Finally, add the data source to JClass Chart. JClass Chart will take care of the rest.
MyDataSource me = new MyDataSource(); c.getDataView(0).setDataSource(me);
class MyModifiableDataSource implements EditableChartable { Vector dataStuff; // Same as before public int getDataInterpretation() { // Return format of the data return Chartable.ARRAY; } // Same as before public java.lang.Object getDataItem(int row, int column) { if (dataStuff == null) return null; Object rval = null; try { rval = (Vector)(dataStuff.elementAt(row)).elementAt(column); } catch (Exception e) { rval = null; } return rval; } // Same as before public java.util.Vector getRow(int row) { if (dataStuff == null) return null; java.util.Vector rval = null; try { rval = (Vector)(dataStuff.elementAt(row)); } catch (Exception e) { rval = null; } return rval; } // Same as before public int getNumRows() { int rval = 0; try { rval = dataStuff.size(); } catch (Exception e) { return rval; } // Same as before public String[] getPointLabels(int row) { // We don't care about point labels return null; } // Same as before public String getSeriesName(int row) { // We don't care about series name return null; } // Same as before public String getName() { // Name of the data view return "MyData"; } // Same as before public String getSeriesLabel(int row) { // Series label and name are the same return getSeriesName(row); } // Set a single item in the data public void setDataItem(int row, int column, java.lang.Object item) { try { if (lockDataItem(row, column)) { dataStuff.elementAt(row).setElementAt(item, column); unlockDataItem(row, column); } } catch (Exception, e) { } } }
As before, add the data source to JClass Chart. JClass Chart will take care of the rest.
MySmartDataSource me = new MySmartDataSource(); c.getDataView(0).setDataSource(me);
class MySmartDataSource extends ChartDataModel { Vector dataStuff; // Same as MyModifiableDataSource public int getDataInterpretation() { } public Object getDataItem(int row, int column) {} public Vector getRow(int row) { } public int getNumRows() { } public String[] getPointLabels(int row) { } public String getSeriesLabel(int row) { } public String getSeriesName(int row) { } public String getName() { } public void setDataItem(int row, int column, java.lang.Object item) { } }
Since MySmartDataSource inherits from ChartDataModel, it is an Observable object, and can make calls to notifyObservers() to inform all views of a change in data. Below are some example calls:
// New row #3 notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.NEW_ROW, 3, -1); // Row #2 has changed notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.CHANGE_ROW, 2, -1); // Value at (5,3) has changed notifyObservers(new ChartDataModelUpdate(ChartDataModelUpdate.CHANGE_VALUE, 5, 3);Note that the data is not sent with the update message. Instead, it is up to JClass Chart to retrieve the data. Note also that JClass Chart is prohibited from modifying the data during a notifyObservers() call.
JCAxis x = c.getDataView(0).getXAxis(); x.setAnnotationMethod(JCAxis.TIME_LABELS_AXIS);
JCAxis y = c.getDataView(0).getYAxis(); y.setAnnotationMethod(JCAxis.VALUE_LABELS); double runlen = y.getMax() - y.getMin(); double inc = runlen / 10; for (int i = 1; i < 10; i++) { y.addValueLabel(new JCValueLabel(y.getMin() + inc*i, new String("Item " + i))); } y.addValueLabel(new JCValueLabel(y.getMin(), new String("Start"))); y.addValueLabel(new JCValueLabel(y.getMax(), new String("End")));
JCAxis ax = new JCAxis(c.getChartArea(), true, JCAxis.VALUE); c.getDataView(1).setYAxis(ax);
c.getDataView(0).setInverted(true);
c.getHeader().setText("[IMAGE=images/duke.gif]", true);
c.getFooter().setText("[HREF=http://www.klg.com]KL GROUP[/HREF]", true);
ChartDataView data = c.getDataView(0); ChartDataViewSeries series = data.getSeries(data.getSeriesIndex("Profit"));
// Shift-drag means zoom c.setTrigger(0, new EventTrigger(Event.SHIFT_MASK, EventTrigger.ZOOM)); // Control-drag means translate c.setTrigger(1, new EventTrigger(Event.CTRL_MASK, EventTrigger.TRANSLATE)); // Drag means edit c.setTrigger(2, new EventTrigger(0, EventTrigger.EDIT));