My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsageExamples  

Featured
Updated Feb 4, 2010 by lewisva...@gmail.com

Drawing a Line Chart

/*
A single data set should be contained in an int[] or a float[]
Use -1 for missing values
*/
int [] data = new int[] { 40, 30, 20, 10, 0 };

// Specify chart width and height in pixels
LineChart chart = new LineChart(150, 150);

// SetData will accept int[] and float[] for single data sets
chart.SetData(data);

string imageUrl = chart.GetUrl();

Adding Axes

int [] data = new int[] { 40, 30, 20, 10, 0 };

LineChart chart = new LineChart(150, 150);
chart.SetData(data);

/*
Create an axis along the bottom of the graph
Labels will be automatically generated by the API using the range values.
*/
ChartAxis bottomAxis = new ChartAxis(ChartAxisType.Bottom);
bottomAxis.SetRange(0, 500);
bottomAxis.HexColor = "00FF00";
bottomAxis.FontSize = 14;
chart.AddAxis(bottomAxis);

/* 
Create an axis along the top of the graph
This time we specify the labels ("text", position)
The default range for an axis is 0-100
*/
ChartAxis topAxis = new ChartAxis(ChartAxisType.Top);
topAxis.AddLabel(new ChartAxisLabel("one", 0));
topAxis.AddLabel(new ChartAxisLabel("two", 75));
topAxis.AddLabel(new ChartAxisLabel("three", 100));
chart.AddAxis(topAxis);

string imageUrl = chart.GetUrl();

Working with multiple data sets

/*
Each data set should be contained in an int[] or float[]
To work the multiple data sets add them to a data structure that implements ICollection
*/
float[] set1x= new float[] { 0, 30, 60, 70, 90, 95, 100 };
float[] set1y = new float[] { 20, 30, 40, 50, 60, 70, 80 };
float[] set2x = new float[] { 10, 30, 40, 45, 52 };
float[] set2y = new float[] { 100, 90, 40, 20, 10 };

List<float[]> dataList = new List<float[]>();
dataList.Add(set1x);
dataList.Add(set1y);
dataList.Add(set2x);
dataList.Add(set2y);

LineChart lineChart = new LineChart(150, 150, LineChartType.MultiDataSet);
lineChart.SetData(dataList);

// Optional : Provide a hex color for each pair of data sets
lineChart.SetDatasetColors(new string[] { "FF0000", "00FF00" });

// Optional : Provide a label for each pair of data sets
lineChart.SetLegend(new string[] {"First", "Second"});

string url = lineChart.GetUrl();

XKCD Example

// These data represent the Y-axis coordinates of the line
// They will be evenly spaced along the X-axis
float[] data = new float[] {35, 30, 26, 22, 17,  5, 96,  5,  4,  3,  2,  2,  1,  1};

// Create the bottom X-axis that will contain the BAC numbers
ChartAxis bottomAxis = new ChartAxis(ChartAxisType.Bottom);
bottomAxis.SetRange(0, 30);

string[] axisLabels = new string[] {".00", ".02", ".04", ".06", ".08", ".10",
                                    ".12", ".14", ".16", ".18", ".20", ".22",
                                    ".24", ".26"};

// The labels are positioned across the X-axis in relation to the axis range
// They will be located at positions {0, 2, 4, 6 ... 28}
for (int i = 0; i < axisLabels.Length; i++)
{
    bottomAxis.AddLabel(new ChartAxisLabel(axisLabels[i], i*2));
}

// Create another bottom X-axis that we will stack under our BAC numbers
ChartAxis bottomAxis2 = new ChartAxis(ChartAxisType.Bottom);
bottomAxis2.AddLabel(new ChartAxisLabel("Blood Alcohol Concentration (%)", 50));

LineChart lineChart = new LineChart(400, 200);
lineChart.SetTitle("Programming Skill", "000000", 14);
lineChart.SetData(data);

// If two axis are set to the same location the first one added will be closest
// to the chart
lineChart.AddAxis(bottomAxis);
lineChart.AddAxis(bottomAxis2);
return lineChart.GetUrl();

See this chart

Comment by raj.chud...@gmail.com, Mar 2, 2008

i have list of over 100 data point and labels. i how do i use the API? i know that the URL will be way too long. is this possible?

Comment by naikro...@gmail.com, May 28, 2010

yes, it's possible.

Comment by mzayarn...@gmail.com, Sep 1, 2010

Pie charts support legend, but it impossible set legend in code...

Comment by andi.sch...@gmail.com, Dec 24, 2010

Hey, Great stuff to create Charts but i have troubles to get the DLL work in C# Visual Studio 2008 Express. I added the DLL to my Project Folder and implemented this code: using GoogleChartSharp?; But the compiler says: namespace GoogleChartSharp? not found. If i double click on the GoogleChartSharp?.DLL the object browser is displaying all methods like BarChart?, PieChart?, and so on. What am i doing wrong? Thanks a lot and have a merry Christmas !! Andi

Comment by NJAldwin, Jan 1, 2011

@andi.schmid1210 Have you added the DLL as a reference? Right click on the "References" folder in the Solution Explorer and click "Add Reference..." then click the Browse tab and browse to the DLL. Click OK and try rebuilding.


Sign in to add a comment
Powered by Google Project Hosting