Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to do a simple bar chart from data in a DGV, "datagridview control". I can get the data from the DGV control, and I have attempted to show it on the chart, but all I get is a white area where the chart should be. I have no idea what I am overlooking. The code I am trying to use is.

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using RegGlobals = FoodPantryGlobals.GlobalConst;
using Food_Pantry;
using OfficeOpenXml;
using Microsoft.Office.Interop.Excel;
using System.Drawing;
using Series = Microsoft.Office.Interop.Excel.Series;


private void ChartSetup()
{
    chartTotals.Visible = true;
    chartTotals.Enabled = true;
    chartTotals.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
    chartTotals.ChartAreas[0].AxisX.Maximum = intEmptyRow - 4;
    chartTotals.ChartAreas[0].AxisX.Minimum = 0;
    chartTotals.ChartAreas[0].AxisY.Maximum = 1000;
    chartTotals.ChartAreas[0].AxisX.Minimum = 0;
    for (int i = 0; i < dgvExcel.RowCount - 2; i++)
    {
        // Get the date from the datagridview
        string strMonth = dgvExcel.Rows[i].Cells[0].Value.ToString();
        // Strip out the month from the datagridview data
        strMonth = strMonth.Substring(0,3);
        // Assign the month to the x axis
        chartTotals.Series[0].AxisLabel = strMonth;
    }
    chartTotals.ChartAreas[0].RecalculateAxesScale();
}


What I have tried:

Several YouTube videos
Articles herein Codeproject
Articles on other coding websites
Posted
Updated 13-Aug-23 16:07pm
Comments
Dave Kreskowiak 13-Aug-23 20:22pm    
So where did you tell the chart what data to use?
PaulaJoannAllen 13-Aug-23 21:26pm    
strMonth is the data I am trying to pass to the xAxis and I thought chartTotals.Series[0].AxisLabel = strMonth; was doing that but I thought wrong. I haven't worked on the bar part yet as I can't get the xAxis part to work.
Dave Kreskowiak 13-Aug-23 21:37pm    
Those are the axis labels, not the data points. If you don't have any data in the series, you have a blank chart. It will not show any labels or axis if you don't tell it what to graph.

For graphing, look at GitHub - kirsan31/winforms-datavisualization: System.Windows.Forms.DataVisualization provides charting for WinForms applications.[^]. They have many different charts, including a simple bar chart.

Download the code, run the sample app, and there are many demos and samples with code to get you started.
 
Share this answer
 
Are you really trying to use Excel Interop to create a chart in your app or are you trying to create the chart in an Excel workbook?

If in your own app, you don't need Excel. You can use the charting classes in the System.Windows.Forms.DataVisualization namespace.
 
Share this answer
 
Comments
PaulaJoannAllen 13-Aug-23 21:18pm    
I am loading the data from Excel into the datagridview control then I want to plot the data contained in that control to the Chart control located in the toolbox in visual studio. I have placed the Chart control on my form and named it chartTotals. The datagridview part works fine but I can't seem to get the chart control part to work.
Dave Kreskowiak 13-Aug-23 21:31pm    
Well, you're never telling the chart control what data to use. You never set the Values property of the series. That's why your chart is always blank. It doesn't know what to graph.

You also cannot tell the chart to use the data in the DGV. You have to have the data in another object, like an array, in order to tell the chart to use it.

You would be much better off using the built-in charting classes in .NET than you would using Excel to do the charting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900