Charting: Fast and Efficient
by Grady Werner


Listing One

public string GenerateFunnelChart()
{
 Size imageDimensions = new Size(640, 480);
 string filename = Guid.NewGuid().ToString("n") + ".jpg";
 string relativeFilename = "~/App_Data/Temp/" + filename;
 string physicalFilename = Server.MapPath(relativeFilename);

 NLicense license = new NLicense(NEVRON_LICENSE_KEY);
 NLicenseManager.Instance.SetLicense(license);
 NLicenseManager.Instance.LockLicense = true;

 using (NChartControl chartControl = new NChartControl())
 {
     chartControl.Settings.RenderDevice = RenderDevice.OpenGL;

     // Add a Header
     NLabel header = chartControl.Labels.AddHeader("Sample Funnel Chart");
     header.TextStyle.FontStyle = new NFontStyle("Verdana", 20, FontStyle.Italic);

     // Add a legend
     NLegend legend = chartControl.Legends[0];
     legend.SetPredefinedLegendStyle(PredefinedLegendStyle.BottomRight);

     // Setup chart 
     NFunnelChart chart = new NFunnelChart();
     chart.LightModel.SetPredefinedLightModel(PredefinedLightModel.SoftFrontal);
     chart.Projection.SetPredefinedProjection(PredefinedProjection.Orthogonal);
     chart.Projection.Elevation = 4;
     chartControl.Charts.Clear();
     chartControl.Charts.Add(chart);
     // Add a new Funnel Series to chart
     NFunnelSeries funnel = (NFunnelSeries)chart.Series.Add(SeriesType.Funnel);
     funnel.BorderStyle.Color = Color.LemonChiffon;
     funnel.Legend.DisplayOnLegend = legend;
     funnel.Legend.Mode = SeriesLegendMode.DataPoints;
     funnel.DataLabelStyle.Format = "<percent>";

     funnel.Values.Add(20.0);
     funnel.Values.Add(10.0);
     funnel.Values.Add(15.0);
     funnel.Values.Add(7.0);
     funnel.Values.Add(28.0);

     funnel.Labels.Add("Awareness");
     funnel.Labels.Add("First Hear");
     funnel.Labels.Add("Further Learn");
     funnel.Labels.Add("Liking");
     funnel.Labels.Add("Decision");

     funnel.FillStyles[0] = new NColorFillStyle(Color.FromArgb(169, 121, 11));
     funnel.FillStyles[1] = new NColorFillStyle(Color.FromArgb(157, 157, 92));
     funnel.FillStyles[2] = new NColorFillStyle(Color.FromArgb(98, 152, 92));
     funnel.FillStyles[3] = new NColorFillStyle(Color.FromArgb(111, 134, 181));
     funnel.FillStyles[4] = new NColorFillStyle(Color.FromArgb(179, 63, 92));

     chartControl.ImageExporter.SaveToFile(physicalFilename, new NSize(imageDimensions), 
           NResolution.ScreenResolution, new NJpegImageFormat());
   }
   return physicalFilename;
}
        



2


