2

I have a chart with type columns. How will I add a background color to the Y Axis? like the gray color in this graph? Is this possible? I am using Chart Control for Winforms from Visual Studio 2015. My data is from SQL.

Code to populate Chart:

chart1.DataSource = dtSalesChart;

foreach (DataRow row in dtSalesChart.Rows)
{
   Series S = new Series(row["Period"].ToString());
   chart1.Series.Add(S);
}

for (int j = 1; j < dtSalesChart.Columns.Count; j++)
  {
    for (int i = 0; i < dtSalesChart.Rows.Count; i++)
      {                            
      chart1.Series[i].Points.AddXY(dtSalesChart.Columns[j].ColumnName, 
      dtSalesChart.Rows[i][j].ToString());
       }
  }

  chart1.Series[0].Color = Color.FromArgb(15, 130, 154);
  chart1.Series[1].Color = Color.FromArgb(117, 193, 205);
  Title title = chart1.Titles.Add("ChartTitle");                 
  title.Text = "Sales By Month";
  title.Font = new Font("Tahoma", 12, FontStyle.Bold);
  title.ForeColor = Color.FromArgb(32, 77, 137);

  chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
  chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.White;
  chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.WhiteSmoke;                                        
  chart1.DataBind();

Current Chart: enter image description here

I want to add the gray background in the Y Axis like below :

graph

TaW
  • 53,122
  • 8
  • 69
  • 111
Zhyke
  • 427
  • 7
  • 22
  • What Graph Control/Library are you using? – Prateek Shrivastava Apr 16 '18 at 09:14
  • What kind of chart? What library? What code are you currently using for your chart? And what does *your* chart currently look like? – musefan Apr 16 '18 at 09:15
  • Not easily. You can set different colors to the ChartArea and to the Chart itself.. You can calculate the area to the left and bottom of the Chartarea and fill it but this is a little advanced.. – TaW Apr 16 '18 at 09:20
  • Take a look at [How do I create a bar chart showing percentages bound to a list of objects?](https://stackoverflow.com/q/12635877/3110834) or [Plotting overlapping column or bar chart](https://stackoverflow.com/a/22710510/3110834) – Reza Aghaei Apr 16 '18 at 09:22
  • edited above to add more details. – Zhyke Apr 16 '18 at 09:22
  • Well simply set `chart1.BackColor = Color.LightGray;` or whatever.. – TaW Apr 16 '18 at 09:25
  • @TaW, I am referring to the background color in Y Axis, I'm not sure how I will call it. Its the Dark Gray color in graph for M, A, M etc. – Zhyke Apr 16 '18 at 09:29
  • Ah, you mean the grey columns above the actual columns? Not there out of the box for Column chart type. You can try to draw them (rather hard) or simply fake them by changing the charttype to StackedColumns. Another workaround is to overlay/underlay the chartarea with an identical one where the columns go to the max. This takes a) setting the chartarea.Position and b) (semi)transparent colors (I have edited the title be clearer) – TaW Apr 16 '18 at 09:34

1 Answers1

3

Short of painting the gray columns, (which is painfully hard to get right) I see two options:

Here is a solution that uses StackedColumn.

You need to do a few things to make it work:

  • You need to know the y value (max) of how far the grey columns should go.

  • You need to add a dummy series for them.

  • You need to set all series to StackedColumn

  • You need to set the special property StackedGroupName so that the series you want to be stacked have the same name.

Here is an example:

enter image description here

Here is the code:

chart3.Series.Clear();
Series s1 = chart3.Series.Add("S1");
s1.ChartType = SeriesChartType.StackedColumn;
Series s2 = chart3.Series.Add("S2");
s2.ChartType = SeriesChartType.StackedColumn;

Series s0 = chart3.Series.Add("S0");
s0.ChartType = SeriesChartType.StackedColumn;
s0.Color = Color.LightGray;
s0.IsVisibleInLegend = false;

s0["StackedGroupName"] = "Group1";
s1["StackedGroupName"] = "Group1";
s2["StackedGroupName"] = "Group2";

for (int j = 0; j < data.Columns.Count; j++)
{
    for (int i = 0; i < data.Rows.Count; i++)
    {
        double vy = Convert.ToDouble(data.Rows[i][j].ToString());
        chart3.Series[i].Points.AddXY(j, vy);
        if (i==0)  s0.Points.AddXY(j, 800 - vy);
    }
}
List<string> mn = new List<string>() { "J", "F", "M", "A" };
for (int i = 0; i < s0.Points.Count; i++)
{
    s0.Points[i].AxisLabel = mn[i];
}

A few more notes:

  • For stacking to work the columns need to have numeric x-values. Make sure to change your code to some useful values!!

  • I simply took the loop index and added labels afterwards. You could also take a month and use a suitable DateTime format. I have also adapted the loop to fit my table layout

  • I didn't add any other styling and I have not used any data binding. Not sure what that line means in your code.

  • The dummy Series s0 gets dummy values calculated from a max - yvalue.

  • I trusted my values and did a Convert without TryParse. Pick your way!

  • The 2nd series s2 is not not in a stacking group with any other series, so it stands alone.

  • The order of the series in the legend is reversed to make them look like the stacks. You can reverse the order in which you add them if you like..

You may want to study this post which dicusses a few common issues with stacking.


Your other option would be to overlay two chartareas precisely and set their points and their colors so that the combined effect looks like your image. Here is an example that overlays two pie-like charts..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Wow! Thank you so much! I will try this one and I will give an update later. – Zhyke Apr 16 '18 at 10:44
  • 1
    Hi TaW, again thank you for this one. I already got it worked. I get the max Value for the Y Axis based on my database value. Then I set `chart1.ChartAreas["ChartArea1"].AxisY.Maximum=intMaxValue`. – Zhyke Apr 16 '18 at 11:20