How to insert data in a chart without inserting it into excel cells

Hi,

using gembox spreadsheet, I created a blank chart, hiding the sheet cells.
I wish I could insert values in the chart through a list of object without having to insert values in cells.

this is my code:

//My list of object
var result = new List<DTO_MACHINE_VARIABLES>();
result = con.Query<DTO_MACHINE_VARIABLES>(plantTorque).ToList();

int numberOfHour = 24;

var chart = worksheet.Charts.Add<ColumnChart>(ChartGrouping.Clustered, "B15", "O32");
chart.SelectData(worksheet.Cells.GetSubrangeAbsolute(0, 0, 2, numberOfHour));

chart.Title.Text = "Report";
chart.Axes.Horizontal.Title.Text = "Hours";
chart.Axes.Vertical.Title.Text = "Test";

chart.SetCategoryLabels("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "010", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24");

worksheet.Cells["A3"].Value = "Plant torque";
worksheet.Cells["A4"].Value = "Plant Speed";

chart.Legend.IsVisible = true;
chart.Legend.Position = ChartLegendPosition.Right;

var valueAxis = chart.Axes.VerticalValue;
valueAxis.NumberFormat = "\"\"#,##0";
valueAxis.Minimum = 0;
valueAxis.Maximum = 6000;
valueAxis.MajorUnit = 500;
valueAxis.MinorUnit = 100;
valueAxis.MajorGridlines.IsVisible = true;
valueAxis.MinorGridlines.IsVisible = true;
valueAxis.MajorTickMarkType = TickMarkType.Outside;
valueAxis.MinorTickMarkType = TickMarkType.Cross;

var printOptions = worksheet.PrintOptions;
printOptions.HorizontalCentered = false;
printOptions.PrintHeadings = false;
printOptions.PrintGridlines = false;
printOptions.FitWorksheetWidthToPages = 1;
printOptions.FitWorksheetHeightToPages = 1;

How can I improve the chart?

Thanks
Yuza

Hi Yuza,

Remove these lines of code:

int numberOfHour = 24;
chart.SelectData(worksheet.Cells.GetSubrangeAbsolute(0, 0, 2, numberOfHour));

worksheet.Cells["A3"].Value = "Plant torque";
worksheet.Cells["A4"].Value = "Plant Speed";

And try using one of the Series.Add overload methods.
For example, something like this:

chart.Series.Add("Plant torque", new int[] { 100, 300, 500, 700, ... });
chart.Series.Add("Plant Speed", new int[] { 200, 400, 600, 800, ... });

I hope this helps.

Regards,
Mario

Thank you for the previous answer!

I’m supposed to input time-based data on a 24-hour basis.
How can I differentiate in order to enter the right value in correspondence of the time to which it is tied?
And if a time such as 12 noon had more than one value how could I do?

Thanks
Yuza

Hi Yuza,

Basically, you just need to change where the data is written.
So, those values that you would usually write into the cells, you now need to provide them to Series.Add method.

Anyway, can you create an Excel file with the desired chart and I’ll let you know what Series.Add method calls you’ll need to recreate those charts?

Regards,
Mario

This is my excel:

var chart = worksheet.Charts.Add(ChartGrouping.Clustered, “B15”, “O32”);

chart.Legend.IsVisible = true;
chart.Legend.Position = ChartLegendPosition.Right;

chart.Title.Text = “Plant torque”;
chart.Axes.Horizontal.Title.Text = “Hours”;
chart.Axes.Vertical.Title.Text = “Percentage”;

chart.SetCategoryLabels(“00”, “01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, “010”, “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”);

Thanks
Yuza

Hi Yuza,

I’m a bit confused about what column chart you’re trying to make.
Can you please create manually (with Microsoft Excel) your desired chart and send it to us?

I’ll investigate it and show you how to create that same chart without referencing cells with it.

Regards,
Mario

This is the example:
image

In the previous answers, I put the Excel where there are dates and values.

The chart represents the values over 24 hours for that day (even if only 13 hours are represented in the chart).

My question is: how can I represent on the chart several values at the same time, such as:
14/04/2023 02:08
14/04/2023 02:18
14/04/2023 02:50
There are 3 values for 2 am time.

but in my context, I will have a list of objects with the date and value inside.

The code:

// List of objects with dates (hours) and values to be inserted in the chart.
// Represented in the image of the sheet excel of the 2 preceding answers
result = con.Query<objectWithDateAndValue>(plantTorque).ToList();

var plantTorqueChart = worksheet.Charts.Add<LineChart>(ChartGrouping.Clustered, "B15", "O32");

plantTorqueChart.Legend.IsVisible = true;
plantTorqueChart.Legend.Position = ChartLegendPosition.Right;

plantTorqueChart.Title.Text = "Plant torque";
plantTorqueChart.Axes.Horizontal.Title.Text = "Hours";
plantTorqueChart.Axes.Vertical.Title.Text = "Percentage";

plantTorqueChart.SetCategoryLabels("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "010", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24");

var red = DrawingColor.FromName(DrawingColorName.Red);
plantTorqueChart.Fill.SetSolid(red);
plantTorqueChart.Series[0].Fill.SetSolid(red);

var valueAxis = plantTorqueChart.Axes.VerticalValue;
valueAxis.NumberFormat = "\"% \"#,##0";
valueAxis.Minimum = 0;
valueAxis.Maximum = 1000;
valueAxis.MajorUnit = 100;
valueAxis.MinorUnit = 100;
valueAxis.MajorGridlines.IsVisible = true;
valueAxis.MinorGridlines.IsVisible = true;
valueAxis.MajorTickMarkType = TickMarkType.Outside;
valueAxis.MinorTickMarkType = TickMarkType.Cross;

// Make the entire sheet print horizontally centered on a single page with headings and gridlines.
var printOptions = worksheet.PrintOptions;
printOptions.HorizontalCentered = false;
printOptions.PrintHeadings = false;
printOptions.PrintGridlines = false;
printOptions.FitWorksheetWidthToPages = 1;
printOptions.FitWorksheetHeightToPages = 1;

Thanks
Yuza

Hi Yuza,

You cannot represent such data with a column chart.
That is why I wanted to check exactly what you’re doing when you are creating an Excel file manually.

Anyway, I believe what you should be looking for is an XY chart (scatter chart).
It would probably be better to first try creating the chart that you want using Microsoft Excel. Then when you have figured out how you want to represent your data you can move on to writing that out via the C# code.

Regards,
Mario

Thanks for the previous answers.

I can ask you how can I show negative values in the chart?

image

            listOfValue[0] = 123;
            listOfValue[1] = 456;
            listOfValue[2] = -4353;
            listOfValue[3] = 556;
            listOfValue[4] = -5677;
            listOfValue[5] = 566;

            totalPlantshutdownChart.SetCategoryLabels(listOfDate[0], listOfDate[1], listOfDate[2], listOfDate[3], listOfDate[4], listOfDate[5]);
            totalPlantshutdownChart.Series.Add("Kw/h", listOfValue);

            var valueAxis = totalPlantshutdownChart.Axes.VerticalValue;
            valueAxis.NumberFormat = "\"\"#,##0";
            valueAxis.Minimum = 0;
            valueAxis.Maximum = 1000;
            valueAxis.MajorUnit = 50;
            valueAxis.MinorUnit = 50;

Regards,
Yuza

Hi Yuza,

You’ll need to change the valueAxis.Minimum value to some negative number.

Or perhaps you could remove the setting of both valueAxis.Minimum and valueAxis.Maximum properties.

Regards,
Mario