Charting

We can easily use charts in Resolver One by combining an external charting library and image worksheets. Here's an example, using the Open Source library ZedGraph.

images/chart-demo-zedgraph.png

How to use charts in Resolver One

To get this running yourself, download the dll-only distribution of ZedGraph. Put the ZedGraph.dll into a directory that is on your PATH.

In the Resolver One 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)

Straight away, you should see a new worksheet labelled CHART (RESULT) which holds the chart.

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

  • Load the ZedGraph assembly.
import clr
clr.AddReference("ZedGraph")
  • Create a chart.
chart = GraphPane(
    RectangleF(0, 0, 640, 480),
    "Graph Title", "X Title", "Y Title")
  • Fill it with some data.
pointPairList = PointPairList()
for x in range(50):
    y = Math.Sin(x / 10.0)
    pointPairList .Add( x, y )

or if you want to use data from the grid:

pointPairList = PointPairList()
sheet1 = workbook["Sheet1"]
for col in sheet1.Cols:
    pointPairList.Add(col[1], col[2])
  • Use the library's capability of retrieving an image.
image = chart.GetImage()
  • Create an image worksheet.
workbook.AddImageWorksheet("CHART", image)

Another Example

# 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; here, by way of comparison with ZedGraph, is 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