Samples
Displaying charts
To display a chart you need to create MxComponent, set it's style and then model, and add it to a Swing container:
MxComponent chart = new MxComponent();
MxFrameChartStyle style = new MxFrameChartStyle();
style.legend.placement = MxStandardConstants.Placement.RIGHT;
chart.setStyle(style);
chart.setModel(new MxSampleChartModel(2,5));
add(chart);
WebCharts3D Designer can automatically generate Java code required to create a chart displayed in it. Simply switch to XML Style pane and from the right mouse button menu select View>>Java. Copy and paste the code into your program. You can also save chart's XML style into a file and then instantiate style in your code.
To completely change the chart's style and/or model use setStyle and setModel functions. In our example, "Bar" and "Pie" radio buttons use the following event handler:
if(e.getSource() == rbBar) {
MxFrameChartStyle style = new MxFrameChartStyle();
style.legend.placement = MxStandardConstants.Placement.RIGHT;
chart.setStyle(style);
} else {
MxPieChartStyle style = new MxPieChartStyle();
style.legend.placement = MxStandardConstants.Placement.RIGHT;
chart.setStyle(style);
}
Updating model
To dynamically update chart's model without recreating the model object you need to use MxStandardChartModel. The instances of this model are returned by all XML, database and import functions. If you created your own model, you can convert it to MxStandardChartModel by calling asStandardModel() function:
chart.setStyle(new MxFrameChartStyle());
chart.setModel(new MxSampleChartModel(2,5).asStandardModel());
To update MxStandardChartModel use functions defined in API section of Model chapter and then call repaint() to refresh the chart. In our example the chart's model is updated once in 200 msecs with random data that creates an illusion of moving chart using the following code:
MxStandardChartModel model = (MxStandardChartModel) chart.getModel();
model.deleteCol(0);
model.insertCol(Integer.toString(3000+count++), new double[] {.....},-1 );
chart.repaint();
Updating style
To update the chart's style without recreating the style object you need to change the chart's styles and then call repaint() method to refresh the chart. In our example the pie chart's angle is increased by 5 degrees every 200 msecs that creates an illusion of rotating pie using the following code:
MxPieChartStyle style = (MxPieChartStyle) chart.getStyle();
style.angle += 5;
chart.repaint();
Processing events
This sample demonstrates processing chart selection changed events. To process a selection change event, add an instance of MxComponentListener to the chart and check the event id for MxComponentEvent.STATE_CHANGED. In our example, the following code is used to update a label with the values of the currently selected element:
chart.addMxComponentListener(new MxComponentListener() {
public void componentChanged(MxComponentEvent event) {
if(event.getID() == MxComponentEvent.STATE_CHANGED) {
MxStandardChartModel model = (MxStandardChartModel) chart.getModel();
label.setText("Selected row: "+ model.getRowSelection()+
" column:"+model.getColSelection());
}
}
});
Using designer
This sample demonstrates using MxDesignPanel for editing charts. In our example, the following code is used to initialize the designer component:
MxComponent chart = new MxComponent();
MxFrameChartStyle style = new MxFrameChartStyle();
style.legend.placement = MxStandardConstants.Placement.RIGHT;
chart.setStyle(style);
chart.setModel(new MxSampleChartModel(2,5));
chart.setSize(300,200);
panel = new MxDesignPanel();
panel.setTarget(chart);
add(panel);
panel.setDividerLocation(300);
Clicking Show XML/Accept button toggles between the designer and XML views. You can modify XML inside XML pane and the chart will be updated with the new style. The following code is used to populate XML and to accept it:
if(text.isVisible()) {
MxWidgetStyle style ;
try { style = MxWidgetStyle.read(area.getText(),null); } catch(Exception ex) {
JOptionPane.showMessageDialog(this,XmlReader.getParseExceptionMessage(ex));
return ;
}
panel.getTarget().setStyle(style);
}
else
area.setText(panel.getTarget().getStyle().toXML());