troubleshooting Question

supplying dynamic data to a chart, c#

Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America asked on
ASP.NETC#.NET Programming
7 Comments1 Solution4096 ViewsLast Modified:
I have developed a new chart using c# with some static data. I also have an existing chart using chart control but it is dynamic and its data is supplied via a stored procedure.

Question: How can I use the stored procedure of the existing chart to supply data for the new chart?

FYI, both charts work okay. I am using vs2010 4.0, c#.

Or alternatively, show me how to apply the settings in the new chart to the existing chart created via chart control. Is this possible?

Thank you.

The code for the new chart developed in c# uses static data but needs to be replaced with the stored proc:
       double[] yValues = { 15, 60, 12, 13 };   //for column chart
        double[] yValues2 = { 30, 32, 30, 0 };   //for line
        string[] xValues = { "September", "October", "November", "December" };

        Chart chart = new Chart();

        Series series = new Series("Default");
        series.ChartType = SeriesChartType.Column;

        Series series2 = new Series("Default2");
        series2.ChartType = SeriesChartType.Line;
        series2.Color = System.Drawing.Color.Red;
        series2.BorderWidth = 5; // this is actually the line width...

        ChartArea chartArea = new ChartArea();
        Axis yAxis = new Axis(chartArea, AxisName.Y);
        Axis xAxis = new Axis(chartArea, AxisName.X);

        chart.Series["Default"].Points.DataBindXY(xValues, yValues);
        chart.Series["Default2"].Points.DataBindXY(xValues, yValues2);
        chart.Series["Default"].Color = System.Drawing.Color.Blue; // change default if you want to
        chart.Series["Default"].Points[0].Color = System.Drawing.Color.Red;
        chart.Series["Default"].Points[chart.Series["Default"].Points.Count - 1].Color = System.Drawing.Color.Orange;


        chart.Width = new Unit(300, UnitType.Pixel);
        chart.Height = new Unit(200, UnitType.Pixel);
        string filename = "C:\\image\\Chart.png";
        chart.SaveImage(filename, ChartImageFormat.Png);
The existing chart using chart control with stored procedure:
    <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" 
        BackColor="Transparent" BorderlineColor="Black" BorderlineDashStyle="Solid" 
        Palette="Pastel" Width="846px" onload="Chart1_Load">
            <asp:Series Name="Series1" XValueMember="ColName" YValueMembers="ColValue" color="lightgreen" >
            <asp:ChartArea Name="ChartArea1" BackColor="White" BackSecondaryColor="Yellow">
                <AxisX Interval="1">
                    <LabelStyle ForeColor="White" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ROD_July18ConnectionString %>" 
        SelectCommand="spMonthlyChart" SelectCommandType="StoredProcedure">
            <asp:SessionParameter DefaultValue="101" Name="Metric_ID" 
                SessionField="Metric_ID" Type="Int32" />
            <asp:SessionParameter DefaultValue="2013" Name="YYYY" SessionField="YYYY" 
                Type="String" />
            <asp:SessionParameter DefaultValue="16" Name="MedCtr_ID" 
                SessionField="MedCtr_ID" Type="Int32" />

The data from the stored proc:
ColName  ColValue       Target
2012	0.8192312	0.7
Jan	0.8309572	0.7
Feb	0.8315933	0.7
Mar	0.8157957	0.7
Apr	0.8275316	0.7
May	0.82	        0.7
Jun	0.8094095	0.7
Jul	0.8114406	0.7
Aug	0.8148234	0.7
Sep	0.8131313	0.7
Oct	0.8088762	0.7
Nov	0.8167956	0.7
Dec	0.8162665	0.7
13-YTD	0.8182085	0.7
Robert Schutt
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros