Charting

This topic shows you how to use ZedGraph and ChartDirector to create charts in Resolver One. Here's a ZedGraph chart:

images/chart-demo-zedgraph.png

You will learn

  • How to create charts by combining an external charting library and image worksheets.
  • How to use ZedGraph, an open source library.
  • How to use ChartDirector, a commercial charting component.

Using a chart in Resolver One

Try the following...

  1. Download the dll-only distribution of ZedGraph.

  2. Put the ZedGraph.dll into a directory that is on your PATH.

  3. Into the Resolver One Post-formula code paste:

    import clr
    clr.AddReference("ZedGraph")
    
    from System import Math
    from System.Drawing import Bitmap, Graphics, RectangleF
    from ZedGraph import GraphPane, PointPairList, SymbolType
    
    chart = GraphPane(
        RectangleF(0, 0, 640, 480),
        "Graph Title",
        "X Title",
        "Y Title" )
    pointPairList = PointPairList()
    for x in range(50):
        y = Math.Sin(x / 10.0)
        pointPairList .Add( x, y )
    
    chart.AddCurve("Sine Wave", pointPairList , Color.Blue, SymbolType.Diamond)
    g = Graphics.FromImage(Bitmap(1, 1))
    chart.AxisChange(g)
    
    image = chart.GetImage()
    workbook.AddImageWorksheet("CHART", image)
    

A new worksheet, CHART (RESULT), appears. This holds the chart.

How the code works

Here's a step-by-step explanation of how the above code works:

  1. Load the ZedGraph assembly:

    import clr
    clr.AddReference("ZedGraph")
    
  2. Create a chart:

    chart = GraphPane(
        RectangleF(0, 0, 640, 480),
        "Graph Title", "X Title", "Y Title")
    
  3. Fill it with some data (this code just plots a sine wave):

    pointPairList = PointPairList()
    for x in range(50):
        y = Math.Sin(x / 10.0)
        pointPairList .Add( x, y )
    

    Or if you wanted to use data from the grid, you could use this code instead to chart the first two columns of the worksheet Sheet1:

    pointPairList = PointPairList()
    sheet1 = workbook["Sheet1"]
    for col in sheet1.Cols:
        pointPairList.Add(col[1], col[2])
    
  4. Use the library's capability of retrieving an image:

    image = chart.GetImage()
    
  5. Create an image worksheet:

    workbook.AddImageWorksheet("CHART", image)
    

Another Example

Here's another example of code for creating a graph using ZedGraph:

# Create a graph of size 640x480
from System.Drawing import Bitmap, Graphics, RectangleF
from ZedGraph import GraphPane, PointPairList, SymbolType

chart = GraphPane(
    RectangleF(0, 0, 640, 480),
    "Performance graph",
    "Revision",
    "Avg Time" )
pointPairList = PointPairList()

# Add data from the the sheet "QueryInput" -
# "revision" and "avg(time)" are headers
for row in workbook['QueryInput'].ContentRows:
    pointPairList.Add(int(row['revision']), float(row['avg(time)']))
chart.AddCurve("Avg time", pointPairList , Color.Blue, SymbolType.Diamond)

# fiddle with the graph scaling and steps
g = Graphics.FromImage(Bitmap(1, 1))

chart.XAxis.Scale.Max = max(int(v)
    for v in workbook['QueryInput'].Cols['revision']
    if v != 'revision') + 3
chart.XAxis.Scale.Min = min(int(v)
    for v in workbook['QueryInput'].Cols['revision']
    if v != 'revision') - 3
chart.XAxis.Scale.MajorStep = 10
chart.AxisChange(g)

# add it to an image worksheet
image = chart.GetImage()
workbook.AddImageWorksheet("CHART", image)

Chart Director

ChartDirector is a commercial charting component. By way of comparison with ZedGraph, here's how you might use it in your Resolver One workbook:

images/chart-demo-director.png
import clr
clr.AddReference("netchartdir")
from ChartDirector import PieChart
from System.Collections import ArrayList

chart = PieChart(300, 300)
chart.setPieSize(150, 140, 100)

data = ArrayList()
labels = ArrayList()

sheet1 = workbook["Sheet1"]

for row in sheet1.Rows:
    labels.Add(str(row[1]))
    data.Add(row[2])

chart.setData(data.ToArray(type(2.0)), labels.ToArray(str))

image = chart.makeImage()
workbook.AddImageWorksheet("CHART", image)

Comments