NetCharts® Pro and a ChartEvent Example
SimpleEventExample demonstrates the use of NetCharts Pro to create, populate and display a chart that listens for events. The program demonstrates the use of a common model for integrating charts into applications. It factors the chart definition into two components - a static component and a dynamic component. The source code is below.

Source Code
/*
SimpleEventExample
Visual Mining, Inc.This program demonstrates the use of NetCharts Pro to create,
populate and display a chart that listening for events.The program demonstrates the use of a common model for integrating
charts into applications. It factors the chart definition into two
components - a static component and a dynamic component.The static component, often referred to as the chart template, contains
that part of the chart definition which does not change from instance
to instance. It includes attributes such as chart style, layout, colors,
fonts and sizes. Chart templates can be quickly created with Visual Mining's
NetCharts Designer, a graphical desktop tool that allows point-and-click
creation of chart definitions. Chart templates can also be created manually.
Chart Templates can be stored in separate disk files, or inlined as
string variables.The dynamic component of a chart definition contains the attributes which
change between invocations. Typically this is the actual data to be plotted,
axis bounds and titles. This data is fetched or computed at runtime and
added to the chart through api calls.The program can be compiled with a command similar to:
d:\jdk1.3\bin\javac -classpath "d:\ncpro.jar;." SimpleEventExample.javaThe program can be run with a command similar to:
d:\jdk1.3\bin\java -classpath "d:\ncpro.jar;." SimpleEventExample
*/package ncpro.examples.standalone;
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;import netcharts.graphics.JGraph;
import netcharts.graphics.NFGraphObserver;
import netcharts.pro.common.*;
import netcharts.pro.charts.bar.NFBarchart;
import netcharts.pro.common.barset.*;
import netcharts.pro.event.*;public class SimpleEventExample implements NFChartScrollListener,
NFChartDragListener, NFChartDwellListener, NFChartGraphListener {
private Label status = null;
private int chartWidth = 600;
private int chartHeight = 350;
private NFBarchart chart = null;
private JFrame f = null;
private String chartTemplate =
"ChartType = BARCHART;"+
"ChartName = \"NetCharts Pro Event Example\";"+
"Background = (white,NONE,4,null,TILE,black);" +
"AntiAlias = ON;"+
"BarWidth = 61;"+
"Bar3DDepth = 0;"+
"Header = (\" \",black,\"Helvetica\",16,0);"+
"HeaderBox = (null,NONE,5,null,TILE,black);"+
"DataAxis = (BOTTOM,LEFT);"+
"Grid = (gainsboro,white,white,null,TILE);"+
"Legend = (\"Support Level \",black,\"Helvetica\",10,0);"+
"LegendLayout = (HORIZONTAL,BOTTOM,0,0,TOPLEFT,-1);"+
"LegendBox = (,BOX,1,,TILE,grey);"+
"RightTitle = (\" \",black,\"Helvetica\",12,0);"+
"LeftTitle = (\"Number\n of \nCalls\n (000's) \",black,\"Helvetica\"
,12,0);"+
"LeftTitleBox = (null,NONE,0,null,TILE,black);"+
"LeftTics = (\"ON\",grey,\"Helvetica\",10,0,null);"+
"LeftColor = grey;"+
"BottomTics = (\"ON\",grey,\"Helvetica\",10,0,null);"+
"BottomColor = grey;"+
"BottomScale = (-0.5, 2.5, 1);"+
"BottomScroll = (-0.5, 4.5);"+
"DataSets = (\"Evaluation\",x008b87,BAR,4,FILLED),(\"Standard\",x99cccc,BAR,4,FILLED),
(\"Platinum\",xe2ebf0,BAR,4,FILLED);"+
"DwellLabel = (,black,\"Helvetica\",12,0);"+
"NoteBox = (white_0,NONE,1,null,TILE,black);"+
"NoteSet1 = (\"Drag Me!\",30,15,null,null,null,null,null,null);"+
"NoteAxis = (PIXEL,PIXEL);"+
"NoteSets = (,CENTER);"+
"NoteLabel = (,red,\"Helvetica\",10,0);";public void go(){
try {
// create a new Barchart bean with the given chart template.
chart = new NFBarchart();// don't update the underlying graphic until we are done.
chart.setAlwaysUpdate(false);// initialize the chart from the base template.
chart.initializeFromString(chartTemplate);
// Set the width and height of the chart.
chart.setSize(chartWidth, chartHeight);// load the dynamic content into the chart.
// At this point the program would likely go off and
// fetch the dynamic portions of the chart definition.
// For this example we'll hardcode that data into
// arrays to simulate data returned from some source
String[] barLabelData = {"Atlanta","Washington","Philadelphia","Boston"};
int[] level0Data = {15,30,35,55};
int[] level1Data = {40,35,25,30};
int[] level2Data = {45,35,40,15};// load the bottom labels into the chart from a vector.
NFVector barLabels = new NFVector();
for (int i=0; i<barLabelData.length; i++) {
barLabels.addElement(barLabelData[i]);
}
chart.setDataLabels(barLabels);// set the bar values.
NFBarSeries bs = chart.getBarSeries();// load level0 support data bar values.
NFBarSet level0 = (NFBarSet)bs.elementAt(0);
for (int i=0; i<level0Data.length; i++) {
level0.addElement(new Integer(level0Data[i]));
}// load level1 support data bar values.
NFBarSet level1 = (NFBarSet)bs.elementAt(1);
for (int i=0; i<level1Data.length; i++) {
level1.addElement(new Integer(level1Data[i]));
}// load level2 support data bar values.
NFBarSet level2 = (NFBarSet)bs.elementAt(2);
for (int i=0; i<level2Data.length; i++) {
level2.addElement(new Integer(level2Data[i]));
}// save the bar series into the chart.
chart.setBarSeries(bs);// customize title for chart.
NFTitle header = (NFTitle)chart.getHeader();
String titleData = "Eastern Region Call Centers";
String titleData2 = "Q2 Call Volume by Type";
header.setText(titleData + "\n" + titleData2);
chart.setHeader(header);// register to receive chart events
chart.addChartActionListener(this);// update the chart with all of the changes.
chart.sendUpdate();// (Debug) print the graph CDL as finally loaded.
System.out.println("Chart CDL:\n");
System.out.println(chart.toString());
System.out.println("\n\n");status = new Label("ChartEvent: NONE");
status.setBackground(java.awt.Color.lightGray);// create the Frame that will contain the chart.
f = new JFrame("Visual Mining NetCharts Pro Barchart Event Example");
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(BorderLayout.CENTER, chart.getPanel());
f.getContentPane().add(BorderLayout.SOUTH, status);
f.setSize(chart.getPanel().getSize());
f.show();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}// This method satisfies the NFChartEventListener interface
// It receives ChartActionEvents and displays textual descriptions
// of them in a status bar below the chart. By default, the specific
// event methods will be called. If no method is appropriate for the
// event, then chartActionPerformed will be called.
public void chartActionPerformed(NFChartActionEvent cae) {
status.setText("chartActionPerformed:"+cae.getType());
}public static void main(String[] args) {
SimpleEventExample example = new SimpleEventExample();
example.go();
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartScrollListener#preScroll(java.lang.Object,
double, double, double, double, double, double)
*/
public boolean preScroll(Object obj, double scrollMin, double scrollMax,
double curScaleMin, double curScaleMax, double newScaleMin, double newScaleMax) {
status.setText("Last Event: SCROLL started with scroll (" + scrollMin + ","
+ scrollMax + ")" +
", current scale of (" + curScaleMin + "," + curScaleMax + ")" +
" and new scale (" + newScaleMin + "," + newScaleMax + ")");
return true;
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartScrollListener#postScroll(java.lang.Object,
double, double, double, double)
*/
public void postScroll(Object obj, double scrollMin, double scrollMax,
double scaleMin, double scaleMax) {
status.setText("Last Event: SCROLL ended with scroll (" + scrollMin + "," +
scrollMax + ")" +
" and scale (" + scaleMin + "," + scaleMax + ")");
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartDragListener#preDrag(java.lang.Object,
double, double, double, double)
*/
public boolean preDrag(Object obj, double curX, double curY, double newX, double newY) {
status.setText("Last Event: DRAG going from (" + curX + "," + curY + ")" + " to
(" + newX + "," + newY + ")");
return true;
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartDragListener#postDrag(java.lang.Object, double, double)
*/
public void postDrag(Object obj, double newX, double newY) {
status.setText("Last Event: DRAG ended at " + " at (" + newX + "," + newY + ")");
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartDwellListener#dwellDisplay(boolean,
netcharts.pro.common.NFActiveLabel)
*/
public boolean dwellDisplay(boolean onoff, NFActiveLabel label) {
if (label.getLabel() != null)
status.setText("Last Event: DWELL on " + label.getLabel());
return false;
}/* (non-Javadoc)
* @see netcharts.pro.event.NFChartDwellListener#dwellPress(java.awt.Event, int, int, netcharts.pro.common.NFActiveLabel)
*/
public boolean dwellPress(Event evt, int x, int y, NFActiveLabel label) {
if (label.getLabel() != null)
status.setText("Last Event: CLICK on " + label.getLabel() + " at (" + x + "," + y + ")");
return false;
}/* (non-Javadoc)
* @see netcharts.graphics.NFGraphObserver#graphDrawn(java.awt.Graphics)
*/
public void graphDrawn(Graphics g) {
// This event is called every time the chart is redrawn, which happens often.
}/* (non-Javadoc)
* @see netcharts.graphics.NFGraphObserver#graphTooSmall(java.awt.Dimension)
*/
public void graphTooSmall(Dimension size) {
status.setText("Last Event: TOO SMALL!!");
System.out.println("Last Event: The size (" + size.getWidth() + "," +
size.getHeight() + ") was TOO SMALL");
}
}
![]()



