Set marker on MSChart at specific X-Value
Here is the code that generates my chart:
System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();
Chart2.Width = 350;
Chart2.Height = 350;
Chart2.RenderType = RenderType.ImageTag;
Chart2.Palette = ChartColorPalette.BrightPastel;
Chart2.ChartAreas.Add("Series 1");
Chart2.ChartAreas["Series 1"].BackColor = System.Drawing.Color.Transparent;
// create a couple of series
Chart2.Series.Add("Series");
// databinding
Chart2.DataSource = pointCollection;
Chart2.ChartAreas[0].AxisX.Title = "Date";
Chart2.ChartAreas[0].AxisY.Title = "Future Exposure Amount";
Chart2.Series[0].ChartType = SeriesChartType.Line;
Chart2.Series[0].XValueMember = "ExposureDate";
Chart2.Series[0].XValueType = ChartValueType.Date;
Chart2.Series[0].YValueMembers = "MaximumExposure";
Chart2.BackColor = System.Drawing.Color.FromArgb(211, 223, 240); //"#D3DFF0"
Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart2.BackGradientStyle = GradientStyle.TopBottom;
// Render chart control
Chart2.Page = this;
Page.Response.Clear();
HtmlTextWriter writer = new Htm开发者_如何学运维lTextWriter(Page.Response.Output);
Chart2.RenderControl(writer);
What is the code to set a marker at a certain X-Value on the chart?
You can set the Marker properties on a point by point basis, e.g.
double interestingValue = 12.34;
foreach (var pt in Chart2.Series[0].Points)
{
if (pt.XValue == interestingValue)
{
pt.MarkerColor = System.Drawing.Color.Red;
pt.MarkerSize = 5;
pt.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
}
}
The XValues will come from the data you bound to, the pointCollection
variable.
If there are dates in the "ExposureDate" of the pointCollection you may be better accessing that directly to find the Date you want, and then using
var pt = Chart2.Series[0].Points[interestingIndex];
to access the DataPoint
精彩评论