Question

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 HtmlTextWriter(Page.Response.Output);
                Chart2.RenderControl(writer);

What is the code to set a marker at a certain X-Value on the chart?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top