A step-through for building a basic NetCharts Pro chart

April 1, 2013
Design Approaches

Sometimes we get asked for the Java code to create a simple chart. Once you’ve done it a few times, you forget it can be useful to have this code the first time. So here’s a step-through for building a basic chart that can be included in a servlet, for instance, and returned to a JSP.


First, create the chart object, and a String to hold the HTML that will be returned:

NFXYchart chart = null;
String chartHtml = null;

Then use our static method to create a chart from a CDL template:

chart = (NFXYchart) NFGraph.getGraphFromTemplate(getDefaultXYChart());

Finally, add a LineSeries (that holds datasets) and a LineSet:

NFLineSeries ls = chart.getLineSeries();
NFLineSet set = (NFLineSet)ls.elementAt(0);

Now it’s time to add data. Here we add four x,y data points to the dataset:


Done-zo! Now we add the dataset to the series, and the series to the chart:

ls.setElementAt(set, 0);

To generate the HTML for a web page, we provide another static method to make things easy:

chartHtml = NFServletUtil.getPage(request, chart, new NFRasterPageParams());

where request is the HttpServletRequest. That’s it! Set a page attribute to the chartHtml and you’re done. We provide the full servlet source here, and a simple JSP here.

Comments are closed.