I'm currently working on building a report in c# and then outputting it to word.
I've got the chart building and being populated by data however one thing I cant get my head around is how to set the chart size / height.
any help would be appreciated, spent hours on this seemingly simple task.
CODE:
Chart CashflowChart = _document.InlineShapes.AddChart(XlChartType.xlColumnClustered).Chart;
CashflowChart.ChartArea.Height = 100;
This results in the following error :
{"Not implemented (Exception from HRESULT: 0x80004001 (E_NOTIMPL))"}
After hours of messing around and contrary to what msdn docs says you cant edit the chartarea directly, you can edit the chartobject which is the parent of the chartarea :
CashflowChart.Parent.width= 550;
CashflowChart.Parent.height = 150;
Related
What I work with: C#, Windows Forms, Visual Studio 2017
I'm trying to get a chart which would show some statistics after selecting a period (for this I have two datetimepickers). And in the chart's X-axis it doesn't show all the values it should, only several values as can be seen in screenshot below. And the Y-axis doesn't even show any values.
(alas, I don't have 10 points of reputation to post images, so I provide the links to images)
First screenshot:
https://i.imgur.com/07ntjI9.png?
In stackoverflow I found some solutions:
chartCheck.ChartAreas[0].AxisX.IntervalType = Your type here;
chartCheck.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
chart.ChartAreas["chart1"].AxisX.Interval = 10;
Based on these, I wrote code for X- and Y-axis intervals and put them into the designer file, but they keep disappearing:
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisY.Interval = 1;
Here's how the part of code looks like in Designer:
chartArea2.AxisY.Title = "Incompliant Half-Hours";
chartArea2.Name = "ChartArea1";
this.chartIncHalfHoursPerPerson.ChartAreas.Add(chartArea2);
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisY.Interval = 1;
With this I can get the X- and Y-axis to show all the values I want, as can be seen in the screenshot below (in the link).
Second screenshot:
https://i.imgur.com/GRCQqaJ.png?
The problem is that this part of code (see below) disappears after some time (or probably after 1-2 program runs, I couldn't figure the moment and reason of their disappearance) and it all goes back to how it was in the first schreenshot (with much less values being shown):
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisY.Interval = 1;
I probably should add some more info:
0) about the program I'm doing
1) about the fact that in other forms similar lines of code do not disappear - they just stay there firmly
2) about my assumptions about what might be a cause to the mysterious disappearance of the code
Part 0:
It's a program that shows some statistics by request in 4 forms. Three of these forms have one chart, while the 4th form has 2 charts.
Part 1:
As stated above, there are 3 other forms each with one chart. They all have a part of code similar to what is in the form with 2 charts (see code and a link with screenshot below).
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisY.Interval = 1;
Third screenshot:
https://i.imgur.com/u16WUMZ.png?
Part 2:
So here's what I think may be the cause:
0) Each chart is connected to the corresponding BindingSource. If it's a form with one chart - it works pretty much OK. But in our case we have 2 charts and each of them is connected to the same BindingSource.
1) The simple fact there are 2 charts in one form?
2) The layout of this form is basically several SplitContainers which were put one above another. In total, 4 SplitContainers. I'm not sure if it's a bad or normal way to set a "foundation" in a form.
3) I tried both Dock (with "Fill") and Anchor (with "Top, Left, Right, Bottom"). Well, Dock works better, but still doesn't solve the problem.
4) Maybe, I shouldn't be adding the code in Designer, but rather in the file where the form's logic opeartions are written.
5) Finally, I wondered if the problem is due to absence of horizontal scrollbar. After all, there are too much surnames on X-axis. I tried to look into properties of SplitContaner and Chart, then googled about how to add a scrollbar. Tried this code, but the scrollbar doesn't even appear:
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisY.Interval = 1;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.ScrollBar.Size = 10;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chartIncHalfHoursPerPerson.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
Well, most of googled websites tell about how to add a scrollbar to the chart (in the chart itself, not under chart and legends)
some parts of winform code are auto generated. There are comments in the files, search for: -
#pragma region Windows Form Designer generated code
Any editing you do in these regions will disappear.
i would like to align the datatable column to MS WinForm chart control legend in x axis like in excel as in the attached image (The tested result of exporting to Excel by using c# and Microsoft.Office.Interop.Excel)
My question is that is there any options/features which MS chart control supports by default to display the chart legend together with datatable as in excel? My users prefer to view in GUI (Winform) instead of exporting to excel.
I have been search through in MSDN as much as I could but still could not find it.
Are there other alternative solutions for this problem?
Table Column Align to MS Chart X-Axis Legend:
Yes and no.
No, there are no options/features that will display the data in a table in the way you show.
But yes, you can write code to achieve that.
I see two options:
You can create a custom legend with cells to hold the table data. See here for a rather similar example!
You can nest the DataGridView, styled to look as you wish, to sit in the chart.
The hard problem in both ways is to make the table align with the data points both initially and also when the chart is being resized.
The code in the link is not doing much, if anything, in this respect.
The 2nd option would have to use the Pre- or PostPaint event anyway to achieve proper positioning, so at least some level of alignment should happen.
Here is an example screenshot:
As you can see I didn't put much effort in styling the DGV. I neither calculated to height not did I add a column to show the Rows/Series colors..
The most interesting code is in the PrePaint event where I
reserve space for the nested DGV
calculate the left and width for the DGV
Here is my code:
private void chart1_PrePaint(object sender, ChartPaintEventArgs e)
{
ca1.Position.Height = 60; // percent of chart!
L1.Position.Y = 90; // percent of chart!
dataGridView1.Parent = chart1;
int y = (int) ca1.AxisY.ValueToPixelPosition(ca1.AxisY.PositionToValue(65));
int x1 = (int) ca1.AxisX.ValueToPixelPosition(ca1.AxisX.Minimum);
int x2 = (int) ca1.AxisX.ValueToPixelPosition(ca1.AxisX.Maximum);
dataGridView1.Top = y;
dataGridView1.Left = x1;
dataGridView1.Width = x2 - x1;
}
L1 is the Legend and ca1 is the ChartArea. This is really just a quick and dirty example that you can code for it but it will take some effort..
Depeding on how flexible and dynamic you want it to work you will have to add (maybe quite a lot) more code..
In my WPF application I allready exported data to Excel to plot them in a radar chart, just to re-import this chart to the application. I need to do this for real-time analysis of the data and with WPF toolkit I couldn't create radar charts yet (I spend lot of time by searching for a solution before I got to Excel).
My problem now is, that I cant change the horizontal axis of the radar chart via C#. In Excel itself I would do that easy by setting up the chart options, but in my code I didn't got a clue yet what to do and also in debugging I didn't find any chart item, that told me more. My code so far is following:
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open(fileName);
Excel.Worksheet worksheet = workbook.Worksheets[1] as Excel.Worksheet;
CreateData(worksheet, sortangle, sortmu);
// Add chart.
Excel.ChartObjects xlCharts = worksheet.ChartObjects() as Excel.ChartObjects;
Excel.ChartObject myChart = xlCharts.Add(120, 10, 300, 300) as Excel.ChartObject;
Excel.Chart chart = myChart.Chart;
chart.ChartType = Excel.XlChartType.xlRadar;
var scale_range = worksheet.get_Range("A1", "A" + sortangle.Count.ToString());
var data_range = worksheet.get_Range("B1", "B" + sortangle.Count.ToString());
chart.SetSourceData(data_range);
chart.HasLegend = false;
// Set chart properties.
chart.ChartWizard(Source: data_range);
So with this I get such a result:
But the rotation axis need to be scaled by 0 to 360 degree, cause the data I process are stored with the information of an angle.
So I tried, by looking to other C# Excel like questions here e.g. this one, adding different lines like chart.SeriesCollection(1).XValues = scale_range; or chart.Axes(Excel.XlAxisType.xlValue).Source = scale_range;. Even by changing the ChartWizard to chart.ChartWizard(Source: data_range, SeriesLabels: scale_range); I didn't got far.
For any suggestions I would be grateful.
EDIT:
By checking the code you may see, that scale_range is not used atm. I also tried to bring it in the data_range item, but then I get two lines.
Okay. Finally got the solution (with help from here) and hope I can help anyone else with the same question. This works for radar charts too. Just had to correct the code for changing the horizontal axis by this:
Excel.Axis xAxis = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.CategoryNames = scale_range;
okay so I have this task to make a chart, that would start the data horizontally.
Meaning 90degrees to the right(always). same as this in the picture below.
is this even possible? I accomplish the above picture using:
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 90;
but the problem is it only works for two data. Other than the 2 data, the pie rotates to its desired behaviour. Like in this image below. which produces 5 data in the chart.
UPDATE
In simple terms I would like to achieve this.
Lucky me! i found the answer with my curiosity.
For the sake of those who are also facing this problem. So the easy answer I was looking for was
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Rotation = -90;
NOTE:
Rotation accept values up until 180 range only.
Because i had this exception error when i tried to put a 270 value.
Exception of type 'System.ArgumentOutOfRangeException' occurred in
System.Web.DataVisualization.dll, but did not handle in the user code
Additional Information: angle of rotation must be specified in the
range of -180 to 180 degrees.
I found another solution here if your chart is not using 3D styles:
c.Series["s1"]["PieStartAngle"] = "270"
The property you are looking for is actually available. On Form.cs (design window) right click on the chart and click properties.
then go to Series->Custom Properties->PieStartAngle.
It's default 0 and it looks like 90. When you change it to 270 your problem will solve.
Btw, you can't write it like
chart1.Series[0].CustomProperties.PieStartAngle = 270;
You only can edit from the design window.
Hi I am using Ms chart control in winforms application for displaying values according to dates
I need to change the x-axis label values(Dates) direction horizantal to vertical
I have searched so many properties but i did not find any solution for this.
Any one help me on this problem
Many Thanks ....
As I understand your question - you are asking how to rotate the chart label to display vertically.
You can rotate the x-axis label as follows:
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
This assumes you have associated your series with the first chart area, which is the default without modification when using the Winforms designer.
The following images shows how the chart would look before the code above is applied, the second image shows how it appears after the code is applied.
Let me know if this is not what you are trying to do and I will post an updated answer.
Before rotation
After Rotation
Edit: Another answer added after my initial post mentions in certain situations it may be important to set chartArea1.AxisX.IsLabelAutoFit = false;
If you have not already done so, get the chart samples from microsoft:
http://archive.msdn.microsoft.com/mschart
Then check the section on Labels
Chart Features > Labels
To answer your question directly, set the angle in LabelStyle, and don't forget to disable autofit
chartArea1.AxisX.IsLabelAutoFit = false;
chartArea1.AxisX.LabelStyle.Angle = 90;