I've written some code that should loop through the dates in a selected date range within a data table and generate a stacked bar chart.
I took a look at: multiple horizontal stacked bar charts using for loop and Plotting a stacked Bar Chart but wasn't sure how to do what I'm trying to do in C# within winforms, though its definitely similar to the charts created in those 2 questions.
The problem I run into is that only 1 bar shows up despite the for loop. So I tried copying and pasting the chunk of code that adds points to the series a few times and now I get more bars on the chart but I don't understand why.
I need the code to add points to the series in question just once each time the for loop is accessed, not multiple times each time the loop is accessed. The for loop is supposed to cycle through the database data (using a previously specified date range) and put that data into the bar chart. How do I do this correctly? I'm fairly new to this so any advice helps. The next step once I fix the loop is to give it the actual values that were pulled from the database but I didn't want to convolute the code before I have this figured out.
(The code below only shows 1 bar on the chart despite there being 6 different series.)
public partial class Charts : Form
{
public Charts()
{
InitializeComponent();
}
private void LaborHrsButton_Click(object sender, EventArgs e)
{
int FromMonth = MBdtpFrom.Value.Month; //MBdtpFrom and To are datetimepickers the user fills to set a date range for the chart generation.
int ToMonth = MBdtpTo.Value.Month;
int FromYear = MBdtpFrom.Value.Year;
int ToYear = MBdtpTo.Value.Year;
// MB 12 Month Labor Chart
MBChart.Titles.Add("Labor Hrs by Month");
var MBChartArea = new ChartArea("");
MBChartArea.AxisX.Title = "MB Type";
MBChartArea.AxisY.Title = "Months";
int MBstartoffset = -5;
int MBendoffset = 5;
for (int yr = FromYear; yr < ToYear + 1; yr++) {
int[] MBmonths = new int[1000];
int i = 0;
if (yr == FromYear)
{
for (int mo = FromMonth; mo <= ToMonth; mo++)
{
MBChart.Series["CEOD"].Points.AddXY(MBdtpFrom.Value.Month, 10);
MBChart.Series["CNR"].Points.AddXY(MBdtpFrom.Value.Month, 23);
MBChart.Series["MPD"].Points.AddXY(MBdtpFrom.Value.Month, 10);
MBChart.Series["Removal"].Points.AddXY(MBdtpFrom.Value.Month, 4);
MBChart.Series["UAR"].Points.AddXY(MBdtpFrom.Value.Month, 54);
MBChart.Series["Water Wash"].Points.AddXY(MBdtpFrom.Value.Month, 61);
//The above chunk of code only generates 1 bar on the chart. When I copy and paste it a few times, I get a few more bars on the chart (same number of bars and the number of times I copy and paste the above chunk of code.) Why?
//10, 23, 10, 4, 54 etc. are placeholders. This is where the database data variable will go.
MBmonths[i] = mo;
i++;
}
}
else if (yr == ToYear) {
for (int mo = FromMonth; mo <= ToMonth; mo++)
{
MBChart.Series["CEOD"].Points.AddXY(MBdtpTo.Value.Month, 150);
MBChart.Series["CNR"].Points.AddXY(MBdtpTo.Value.Month, 250);
MBChart.Series["MPD"].Points.AddXY(MBdtpTo.Value.Month, 350);
MBChart.Series["Removal"].Points.AddXY(MBdtpTo.Value.Month, 450);
MBChart.Series["UAR"].Points.AddXY(MBdtpTo.Value.Month, 550);
MBChart.Series["Water Wash"].Points.AddXY(MBdtpTo.Value.Month, 650);
MBmonths[i] = mo;
i++;
}
else
{
// ..... similar code
}