Hey, I'm trying to read a database and display it as a graph. I have accomplished this but I have an issue.
Database structure is one table called num1.
num1 only has ID, X, and Y columns. That's it.
The graph displays right but the tags at the bottom have all shifted over right so it's not lined up.
The process I did was, read a database into a dataset, convert it to XML, then display it as a graph.
http://i88.photobucket.com/alb...icemike_2006/chart.png
^ There's the picture.
Code is attached.
Code.......
private void BuildCharts ()
{
//Create a SQL connection
String cn = ConfigurationManager.ConnectionStrings["OWCDEMOConnectionString"].ConnectionString;
//The SQL query
string sqlStatement = "SELECT x, y FROM [num1]";
//Create a new instance of an SqlConnection and assign our connection string to it
SqlConnection sqlConnection = new SqlConnection(cn);
//Open our connection
sqlConnection.Open();
//Create a new instance of a dataset object
DataSet dataSet = new DataSet();
//Assign the SQL statement and connectio to use for this operation
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlStatement, sqlConnection);
//Fill our dataset with the data accumulated from the SQL statement
sqlAdapter.Fill(dataSet);
//ChartSpaceClass oChartSpace = new ChartSpaceClass();
System.IO.StringWriter sw = new System.IO.StringWriter();
XmlDocument xDoc = new XmlDocument();
dataSet.WriteXml(sw);
//Disgard connections
sqlConnection.Close();
dataSet.Dispose();
sw.Close();
//XML
xDoc.LoadXml(sw.ToString());
System.Xml.XmlNodeList nodes;
nodes = xDoc.ChildNodes.Item(0).ChildNodes;
int nCount = nodes.Count + 1;
string[] aNames = new string[nCount];
string[] aTotals = new string[nCount];
string names = String.Empty;
string totals = String.Empty;
int i = 1;
for (i = 1; i < nCount; i++)
{
//aNames = nodes.Item(i - 1).ChildNodes.Item(1).InnerText; //ChildNodes.Item(1) For Car Names
aTotals = nodes.Item(i - 1).ChildNodes.Item(0).InnerText; //ChildNodes.Item(0) For Numbers
}
int j = 0;
for (j = 1; j < nCount; j++)
{
aNames[j] = nodes.Item(j - 1).ChildNodes.Item(1).InnerText; //ChildNodes.Item(1) For Car Names
//aTotals = nodes.Item(i - 1).ChildNodes.Item(0).InnerText; //ChildNodes.Item(0) For Numbers
}
names = String.Join("\t", aNames);
totals = String.Join("\t", aTotals);
//Test Labels
//Convert.ToString(aNames); Label = System.String
//Convert.ToString(nodes); Label = System.Xml...
Label1.Text = names;
Label2.Text = totals;
Label3.Text = Convert.ToString(sw);
Label4.Text = Convert.ToString(nCount);
//**********************************
Microsoft.Office.Interop.Owc11.ChartSpaceClass newChartSpace = new Microsoft.Office.Interop.Owc11.ChartSpaceClass();
Microsoft.Office.Interop.Owc11.ChartChartTypeEnum newChartType;
newChartSpace.Charts.Add(0);
newChartSpace.Charts[0].SeriesCollection.Add(0);
newChartSpace.Charts[0].SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories, Convert.ToInt32(Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral), names);
newChartSpace.Charts[0].SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues, Convert.ToInt32(Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral), totals);
byte[] byteArr = (byte[]) newChartSpace.GetPicture ("png", 800, 500);
//------------------------------------------------------------------------
//Store the chart image in Session to be picked up by an HttpHandler later
//------------------------------------------------------------------------
HttpContext ctx = HttpContext.Current;
string chartID = Guid.NewGuid().ToString();
ctx.Session [chartID] = byteArr;
imgChart.ImageUrl = string.Concat ("chart.ashx?", chartID);
}
}
}