简体   繁体   中英

Microsoft Chart Control Axis Titles not displaying properly

I am having problems getting the XAxis to display the correct values in my chart control. Below is how I am generating each series to be displayed. I would like the XAxis to display the DataType value from s.DataType:

 foreach (SummaryData s in summaryData)
    {
        System.Web.UI.DataVisualization.Charting.Series series = new System.Web.UI.DataVisualization.Charting.Series(s.DataType);
        series.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;            
        DataPoint dp = new DataPoint();            
        dp.Name = s.DataType;            
        dp.SetValueY(s.Total);
        dp.SetCustomProperty("DataType", s.DataType);
        series.XValueMember = "DataType";                        
        series.Points.Add(dp);
        msBarVertLegRight.Series.Add(series);            
    }
    msBarVertLegRight.DataBind();     

The corect value is displayed and the correct name in the Legend, but I'm not exactly sure how to set the XAxis value.

JH

Looks like you're trying to use databinding (which is what the XValueMember is for) and add points one by one. You can do one or the other. You could use databinding like so (approximately):

series.XValueMember = "DataType";
series.YValueMember = "ThePropertyWithY";
series.DataSource = s;
series.DataBind();

or you could set each point individually:

System.Web.UI.DataVisualization.Charting.Series series = new System.Web.UI.DataVisualization.Charting.Series("mySeries");
series.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;

foreach (SummaryData s in summaryData)
{                
    series.AddXY(s.DataType, s.Total);
}

I'm not sure why you had each SummaryData.DataType being its own series; if that is needed, add that back in, but from the code you posted it didn't seem necessary.

When databinding, any changes to your underlying data (the SummaryData objects) will be reflected in your chart automatically; if you add points individually, you need to handle updates to your chart manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM