?
Solved

MSChart Multiline only showing 1st line

Posted on 2011-05-13
4
Medium Priority
?
1,707 Views
Last Modified: 2012-05-11
Hi Experts,

Im trying to do an Mschart multi line graph.
The data behind this graph is based on sales made by user, so over time additional users may be added (ie the total number of lines required is unknown).
I use a stored procedure to pull the data back , this returns it the right way for lines eg as columns of data not rows.

Ive attached the page code. This just sets up the chart itself but provides no series.
In my page load event I have a FOR NEXT loop that goes through and adds the series. This partially works. The loop must be completing correctly as all data shows correctly in the legend, however only series 1 is actually plotted. Is there a way to force the plotting?

thanks

GMM
<asp:Chart ID="Chart1" runat="server" 
        BackColor="211, 223, 240" Height="600px" 
            Width="1000px" BorderDashStyle="Solid" BackGradientStyle="TopBottom" 
            BorderWidth="2px" BorderColor="#1A3B69" IsSoftShadows="False" 
             ImageType="Jpeg" >
             <Series>

             </Series>
             
            <Legends>
                <asp:Legend Name="Legend1">
                </asp:Legend>
            </Legends>
             
        <borderskin SkinStyle="Emboss"></borderskin>
          
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1" BackColor="102, 204, 255" 
                    BackGradientStyle="HorizontalCenter">
                    
                    <AxisY2 IsLabelAutoFit="False">
                        <LabelStyle Font="Microsoft Sans Serif, 15.75pt" />
                    </AxisY2>
                    <AxisY IsLabelAutoFit="False">
                        <MajorGrid Enabled="False" />
                        <LabelStyle Font="Microsoft Sans Serif, 5pt" />
                    </AxisY>
                    <AxisX Interval="1" IntervalAutoMode="VariableCount">
                        <MajorGrid Enabled="False" />
                        <LabelStyle Angle="90" />
                    </AxisX>
                    <Area3DStyle Inclination="20" />
                </asp:ChartArea>
            </ChartAreas>
            <Titles>
                <asp:Title Name="Title1" Text="Running Contractor Report" Font="Trebuchet MS, 10.25pt, style=Bold" 
               >
                </asp:Title>
            </Titles>
        </asp:Chart>

Open in new window

Imports System.Web.UI.DataVisualization.Charting
Imports System.Data.SqlClient
Imports System.Data


Partial Class multiLineChart
    Inherits System.Web.UI.Page

    Sub page_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connStr As String = ConfigurationManager.ConnectionStrings("aspnetdbConnectionString").ConnectionString
        Dim SqlConn As New SqlClient.SqlConnection
        Dim SQLCmd As New SqlCommand
        Dim columnCountTotal
        Dim columnCount
        Dim columnName As String
        SqlConn.ConnectionString = connStr
        SqlConn.Open()
        SQLCmd.CommandText = "getRCRX" ' Stored Procedure to Call
        SQLCmd.CommandType = CommandType.StoredProcedure 'Setup Command Type
        SQLCmd.Connection = SqlConn 'Active Connection
        SQLCmd.Parameters.AddWithValue("@selectedYear", 2011) 'pass through cand id
        SQLCmd.ExecuteNonQuery()
        Dim reader As SqlDataReader = SQLCmd.ExecuteReader() 'pass results to data reader

        columnCountTotal = reader.FieldCount


        For index As Integer = 3 To (columnCountTotal - 1)
            columnName = reader.GetName(index).ToString()
            Chart1.Series.Add(columnName)
            Chart1.Series(columnName.ToString()).ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line
            Chart1.Series(columnName.ToString()).Points.DataBindXY(reader, "label", reader, columnName.ToString())
            Chart1.Series(columnName.ToString()).IsVisibleInLegend = True
            Chart1.Series(columnName.ToString()).IsValueShownAsLabel = True
            Chart1.Series(columnName.ToString()).ToolTip = "Data Point Y Value: #VALY{G}"
            Chart1.Series(columnName.ToString()).ChartArea = "ChartArea1"


        Next

      
        SqlConn.Close()
    End Sub

End Class

Open in new window

0
Comment
Question by:gingermoleman
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
Rouchie earned 2000 total points
ID: 35830351
I still have the Dundas package, but the logic should still be the same.  You are not declaring each Series as a new series, so your code just creates one, then wipes over it with the next one.

In the code I used to do something similar last year, I created a NEW series each time.  So try this and see if it works any better...

' add each series first

For index As Integer = 3 To (columnCountTotal - 1)
	Dim s as New System.Web.UI.DataVisualization.Charting.Series  ' <-- might be different for ASP.NET 3.5
	s.Name = reader.GetName(index).ToString()
	s.Type = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line
	Chart1.Series.Add(s)
Next

' then populate with data

For index As Integer = 3 To (columnCountTotal - 1)
	Dim s as System.Web.UI.DataVisualization.Charting.Series = Chart1.Series(reader.GetName(index).ToString())
	s.Points.DataBindXY(reader, "label", reader, columnName.ToString())
	s.IsVisibleInLegend = True
	s.IsValueShownAsLabel = True
	s.ToolTip = "Data Point Y Value: #VALY{G}"
	s.ChartArea = "ChartArea1"
Next

Open in new window

0
 
LVL 5

Author Comment

by:gingermoleman
ID: 35860497
Hi Rouchie,

I appreciate your input, and for those that follow your solution is good but I dont agree with your explanation.
Firstly I get the 1st column of data show not the last (id expect the last column if each column was replacing the previous one), secondly I cant see this flaw in my code

 For index As Integer = 3 To (columnCountTotal - 1)

** I SETUP AND ADD COLUMN HERE **
            columnName = reader.GetName(index).ToString()
            Chart1.Series.Add(columnName)

** DATA ADDED HERE **
            Chart1.Series(columnName.ToString()).ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line
            Chart1.Series(columnName.ToString()).Points.DataBindXY(reader, "label", reader, columnName.ToString())
            Chart1.Series(columnName.ToString()).IsVisibleInLegend = True
            Chart1.Series(columnName.ToString()).IsValueShownAsLabel = True
            Chart1.Series(columnName.ToString()).ToolTip = "Data Point Y Value: #VALY{G}"
            Chart1.Series(columnName.ToString()).ChartArea = "ChartArea1"

** THIS NEXT LOOP ?SHOULD? MOVE ME TO NEXT COLUMN
        Next

So I dont understand why adding all columns first before adding any data makes a difference, BUT it does! Maybe a render phase takes place when series data is added and therefore the page load cycle is moved forward?? Id love to understand why this is, but ultimately Ive got what I needed.

Thanks again

GMM
0
 
LVL 5

Author Comment

by:gingermoleman
ID: 35860513
Sorry, the solution provided IS correct. Defining the series as NEW then adding it was correct
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35860663
Hi

Thanks for your feedback.

>> So I dont understand why adding all columns first before adding any data makes a difference, BUT it does!

I never grasped that one either, and it took me a LONG time to figure out the solution.  Anyway, I'm glad you got it working... :-)
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question