Ask a questionAsk a question
 

Answerusing DataView with asp.net Chart control

  • Thursday, November 05, 2009 7:21 AMPatrick.I Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can i use this table below to plug a pie chat?

    stateSumPostVoiceSumPostDataSumPreVoiceSumPreData
    National 324 42 9 2

    My DataView is

     

     

    DataView PieDv = PieTable.DefaultView;

    Chart2.Series[

     

    "Default"].Points.DataBindXY(PieDv, "State", PieDv, "SumPostVoice");

    Chart2.Series[

     

    "Default"].Points.DataBindXY(PieDv, "State", PieDv, "SumPostData");

    Chart2.Series[

     

    "Default"].Points.DataBindXY(PieDv, "State", PieDv, "SumPreVoice");

    Chart2.Series[

     

    "Default"].Points.DataBindXY(PieDv, "State", PieDv, "SumPreData");

     

     

     

     


    When i load my chart i just get one big round doughnut color and its just says national.
    I taught it was going to divide it into 100%.

    Thanks in Advance

Answers

  • Friday, November 06, 2009 8:05 AMDhruv Patel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The legends take binding from supplied table's column name also, if you don't know.

    Try something like below :

     

    SomeChart.Series[0].Legend = PieTable.Columns[0].ColumnName;


    If you find solution from my answer, please mark it as answer.

All Replies

  • Thursday, November 05, 2009 8:03 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    Normally you would use the values in a single column

    If you want to use the values in 1 row, you have to iterate through all the column values

    Dim dtTest As New DataTable
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dtTest.Columns.Add("State", GetType(String))
            dtTest.Columns.Add("Sum_Post_Voice", GetType(Integer))
            dtTest.Columns.Add("Sum_Post_Data", GetType(Integer))
            dtTest.Columns.Add("Sum_Pre_Voice", GetType(Integer))
            dtTest.Columns.Add("Sum_Pre_Data", GetType(Integer))
            DataGridView1.DataSource = dtTest.DefaultView
            dtTest.Rows.Add("National", 324, 42, 9, 2)
    
        End Sub
    
        Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click
            
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Doughnut
            Chart1.Series(0).IsValueShownAsLabel = True
            For i As Integer = 1 To dtTest.Columns.Count - 1
                Chart1.Series(0).Points.AddY(dtTest.Rows(0).Item(i))
            Next
    
    
        End Sub
    
    • Proposed As Answer byHammerGuy Friday, November 06, 2009 6:57 AM
    •  
  • Friday, November 06, 2009 4:19 AMPatrick.I Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Jwavila,
                        But  i would also need the x axis which is the column name.
    How can i get that?
    Thx in Advance
  • Friday, November 06, 2009 6:11 AMCole Brand Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm sorry, can you explain why you need two axis to draw a circle. Last time I checked a pie chart only had one real dimension.

    For the purists who can't wait to chime in, yeah, I know any graph takes up space in two dimensions, but we're talking about the data to fuel the chart itself, yeah?
    Please don't forget (and feel free to remind me) to post if you got the answer you wanted, and select who really answered your post when you do so future visitors will know too! Remember, this is .NET 4.0 in a .NET 3.5 world, you're a pioneer right now.
  • Friday, November 06, 2009 6:49 AMPatrick.I Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Cole thanks for the trply.
    Yeah you are right about needing only one axis.
    Sorry what i want is to show the label text on the pie chat.
    Right now i can see the pie chart divided  and all good but i want label text.

    How can i get the columns in my data as above to show as a label.

    If not can i hardcode it e.g like so
    string[] xValues = { "SumPostVoice", "SumPostData", "SumPreVoice", "SumPreData" };

    Hope you understand me now and thks in Advance
  • Friday, November 06, 2009 6:56 AMHammerGuy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I think he means that he wants to have the column name in the description of the pie chart. just check into more details of the ms chart i never worked with ms charts but i have used devexpress charts and in that we had a wizard through which you could do it easily. I think u will be able to find it if u digg a little deeper.

    Wish you luck

    HG
  • Friday, November 06, 2009 7:17 AMPatrick.I Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Thanks hammer thats exactly what i want.

     Cole,hammer 
      i noticed i can use the 
     Chart2.Series["Default"].Label = "#VAL{#}";
     to add the label to display on the pie chart

    But this is where i have the proble with the legend text
     Chart2.Series["Default"].LegendText = "#AXISLABEL";
    when i use the above  the legend shows 324, 42, 9, 2

    I need a text for the legend unless it looks funny.
    How can i get the column names or even hardcode the values dynamically

    My code below:

    for (int i = 0; i <= PieTable.Columns.Count - 1; i++)
                {
                  
                    Chart2.Series["Default"].Points.AddY(PieTable.Rows[0].ItemArray[i]);
                    Chart2.Series["Default"].LegendText = "#AXISLABEL";
                    Chart2.Series["Default"].Label = "#VAL{#}";
                   //How can i get the column names
             }


    Sure i tried doing :
      foreach (DataColumn column in PieTable.Columns)
            {
               
                           Chart2.Series["Default"].LegendText = column.ToString();
            } 
    But no cigar.
    Thks
  • Friday, November 06, 2009 7:23 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    in your loop through the columns, you can get the column name from each. But since you are skipping the first column for the data, the first point is using the data from the second column. So you just have to offset the index by 1.

    this also has some code to add a Title to your Chart, using the value in the first column, so the data in the "State" column shows up on the chart


    Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click
            Chart1.Series(0).Points.Clear()
            Chart1.Titles.Clear()
    
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pie
            Chart1.Series(0).IsValueShownAsLabel = True
            Chart1.Titles.Add(dtTest.Rows(DataGridView1.SelectedRows(0).Index).Item(0).ToString)
            For i As Integer = 1 To dtTest.Columns.Count - 1
                Chart1.Series(0).Points.AddY(dtTest.Rows(DataGridView1.SelectedRows(0).Index).Item(i))
                Chart1.Series(0).Points(i - 1).LegendText = dtTest.Columns(i).ColumnName.ToString
            Next
    
    End Sub
    
  • Friday, November 06, 2009 8:05 AMDhruv Patel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The legends take binding from supplied table's column name also, if you don't know.

    Try something like below :

     

    SomeChart.Series[0].Legend = PieTable.Columns[0].ColumnName;


    If you find solution from my answer, please mark it as answer.