Friday, July 5, 2013

Create 3D Scatter Chart for Winform

There are some tutorial on CodeProject that shows how to create 3D scatter chart (e.g. the one frequently seen in MATLAB), but they are quite primitive. ChartDirector (http://www.advsofteng.com/) seems to offer an excellent solution for this. Below is the piece of code that adds a 3D scatter chart to a ChartDirector win chart viewer after it is added to a Winform:

public void Create3DScatterChart(WinChartViewer viewer, string title, List<Point> plot, int width, int height)
{
try
{
ThreeDScatterChart c = new ThreeDScatterChart(width, height);

c.addTitle(title, "Times New Roman Italic", 10);

c.setPlotRegion(width / 2 - 10, height / 2 - 15, width / 2, width / 2, height / 2 - 10);

c.setViewAngle(15, 30);

c.addLegend(width - 60, height - 180);

double[] xData=new double[plots.Count];
double[] yData=new double[plots.Count];
double[] zData=new double[plots.Count];
for(int i=0; i < plot.Count; ++i)
{
xData[i] = plot[i].X;
yData[i] = plot[i].Y;
zData[i] = plot[i].Z;
}

ThreeDScatterGroup g = c.addScatterGroup(xData, yData, zData, 
"Legend1",
ChartDirector.Chart.GlassSphere2Shape, 13, 
0xFF0000);
g.setLegendIcon(15, 15, 0xFF0000);

// Set the x, y and z axis titles using 10 points Arial Bold font
c.xAxis().setTitle("X", "Arial Bold", 10);
c.yAxis().setTitle("Y", "Arial Bold", 10);
c.zAxis().setTitle("Z", "Arial Bold", 10);

// Output the chart
viewer.Chart = c;


//include tool tip for the chart
viewer.ImageMap = c.getHTMLImageMap("clickable", "", "title='(X={x|p}, Y={y|p}, Z={z|p})'");
}
catch
{

}
}

To instead save the scatter 3d plot to a file, use the following code:

public void Create3DScatterChart(string filename, string title, List<Point> plot, int width, int height)
{
try
{
ThreeDScatterChart c = new ThreeDScatterChart(width, height);

c.addTitle(title, "Times New Roman Italic", 10);

c.setPlotRegion(width / 2 - 10, height / 2 - 15, width / 2, width / 2, height / 2 - 10);

c.setViewAngle(15, 30);

c.addLegend(width - 60, height - 180);

double[] xData=new double[plots.Count];
double[] yData=new double[plots.Count];
double[] zData=new double[plots.Count];
for(int i=0; i < plot.Count; ++i)
{
xData[i] = plot[i].X;
yData[i] = plot[i].Y;
zData[i] = plot[i].Z;
}

ThreeDScatterGroup g = c.addScatterGroup(xData, yData, zData, 
"Legend1",
ChartDirector.Chart.GlassSphere2Shape, 13, 
0xFF0000);
g.setLegendIcon(15, 15, 0xFF0000);

// Set the x, y and z axis titles using 10 points Arial Bold font
c.xAxis().setTitle("X", "Arial Bold", 10);
c.yAxis().setTitle("Y", "Arial Bold", 10);
c.zAxis().setTitle("Z", "Arial Bold", 10);

// Output the chart
using(Image img=c.makeImage())
  {
      img.Save(filename);
   }


}
catch
{

}
}

3 comments:

  1. This is great, but are you aware of any solutions that don't involve paid software?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. If you are wondering what a scatter plot shows, the answer is simpler than you think. The scatter plot also has other names, such as a scatter plot, scatter plot, and correlation plot.
    https://ppcexpo.com/blog/scatter-plot-examples

    ReplyDelete