When the Microsoft chart control is zoomed in, the values of the labels and grid lines often looks like 38.2, 39.2, 40.2, 41.2 ... rather than 38,39,40,41. I tried to set the interval offset to correct this by using the code shown below without success.
myChart.ChartAreas[0].AxisX.MinorGrid.IntervalOffset = myChart.ChartAreas[0].AxisY.ScaleView.ViewMinimum % 1;
myChart.ChartAreas[0].AxisX.IntervalOffset = myChart.ChartAreas[0].AxisY.ScaleView.ViewMinimum % 1;
What is the proper way to get the major and minor grid-lines and labels to be drawn starting at a specific value?
Your problems might be caused by margins being added. Make sure you set
myChart.ChartArea[0].AxisX.IsMarginVisible = false;
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.
Is there a way to add real padding to migradoc table cells? The options that they currently have (LeftPadding and RightPadding) actually work like margins and not like padding. And if you use it, it pushes the background color as well instead of pushing the content only.
This is what happens when you add "padding" to the whole row or individual cells (added it to the first row):
By default it looks like this:
If you wish to remove the white space between the columns, it seems like you have to do it by setting these padding properties to 0 and then you get this:
..which is almost what I want, but I want to push the content of the cell a bit to towards the centre of the cell from all 4 sides so that it looks like this and has a little room to "breathe":
It was even asked on their forums a long time ago about whether this could be done, but the answer doesn't solve the issue at all. It simply mentions the padding properties which work the exact opposite way of what the OP in that thread asked for (which is the same thing I want to do).
I'm using PDFsharp-MigraDoc-gdi v1.50.4000-beta3b nuget package.
Any ways to hack around this odd behaviour? Thanks.
EDIT: updated with a piece of code I use and with added bit recommended by PDFsharp Novice
var table = new Table();
var columnCount = 4;
for (int i = 0; i < columnCount; i++)
{
table.AddColumn();
}
var hedingRow= table.AddRow();
hedingRow.Format.Font.Bold = true;
hedingRow.Format.Font.Color = Color.Parse("0xFFFFFFFF");
hedingRow.Format.Shading.Color = Color.Parse("0xFF005aa0");
hedingRow.HeadingFormat = true;
hedingRow.Cells[0].AddParagraph("Field");
hedingRow.Cells[1].AddParagraph("Type");
hedingRow.Cells[2].AddParagraph("Default");
hedingRow.Cells[3].AddParagraph(String.Empty);
// Doesn't work as I would assume it should based on PDFsharp Novice
hedingRow.Cells[0].Format.LeftIndent = 4;
hedingRow.Cells[0].Format.RightIndent = 4;
hedingRow.Cells[0].Format.Shading.Color = Color.Parse("0xFF005aa0");
You can set the background color for the cell and/or for the text in the cell.
If you set the color for the cell, the padded area will also have the color.
If you set the color the text only, the padded area will have no color.
I have created a chart in the image below and I need to insert in addition to the existing labels, the end labels on the extreme left and right of the x-axis. There are numerous datapoints and therefore I cannot set the interval to 1 for the labels as they'll clutter the axis. I have already tried setting the property
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
but it doesn't seem to work. How can I achieve this?
You may try:
chartHistory.ChartAreas[0].AxisX.IsMarginVisible = true;
chartHistory.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
Or probably you will have to add the labels yourself,
you can either use DataPoint.AxisLabel:
chartHistory.Series[0].Points[0].AxisLabel = "5/4/2010";
or more flexibly:
chartHistory.Series[0].Points[0].AxisLabel =
System.DateTime.FromOADate(chartHistory.Series[0].Points[0].XValue).ToShortDateString();
(and the same for the last point in series)
or you can add a custom label to the AxisX control:
chartHistory.ChartAreas[0].AxisX.CustomLabels.Add(0, 20, "5/4/2010");
See also this answer and this answer.
My chart has a Datetime type X Axis and Double type Y Axis. I am trying to use the following code to insert the scalebreakstyle feature but it is not working. Does anyone have sample code for it. I was trying the web.UI code but did not work. Also I set the color of 2 series the same it keeps making them different.
/
/ Enable scale breaks.
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.Enabled = true;
// Show scale break if more than 25% of the chart is empty space.
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.CollapsibleSpaceThreshold = 25;
// Set the line width of the scale break.
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.LineWidth = 2;
// Set the color of the scale break.
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.LineColor = Color.Red;
// If all data points are significantly far from zero, the chart will calculate the scale minimum value.
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.StartFromZero = StartFromZero.Auto;
// Set the spacing gap between the lines of the scale break (as a percentage of the Y-axis).
chart1.ChartAreas["ChartArea1"].AxisY.ScaleBreakStyle.Spacing = 2;
www.tinypic.com/r/6e2c83/5
Scale breaks will not work under a wide array of conditions. According to the documentation any of the following causes scale breaks to be "not supported", which I have found means they will not work at all or they will warp your graph in weird ways
•Pie, doughnut, funnel, pyramid, radial or any stacked chart types are used.
•Custom intervals for labels, tick marks or grid lines are enabled.
•The minimum or maximum value for the axis is set.
•Custom labels are used.
•A logarithmic Y-axis is specified.
•Axis views on the Y-axis, which include scrolling and zooming, are used.
•3-D charts are used.
It looks like you maybe have a custome x axis label which would cause the scale break to fail.
I am trying to construct a chart using the native ASP.NET 4.0 charting control.
I've done nearly everything I wanted to do, but if you look at the following screenshot:
You'll notice that the Y axis labels are all wonky - that is, they are decimals, and they don't fall directly on any of the actual gridlines.
The purpose of my chart is to show a value that will always be between 0 and 16. I need to know the exact value, so I was able to set the gridlines to represent each value, by using this code:
Chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 1;
As you can see, it is showing all lines, 0 to 16. However, the Y axis labels not only do not line up, but aren't even whole values. I would like there to be a label for EACH gridline, and I'd like them to be whole values.
I've done my share of googling, but I mostly find stuff pertaining to turning off the gridlines altogether, which is not what I want.
Any ideas?
can you not just set AxisY.Interval?
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 1;
chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1;
I created a test chart quick and got the following:
When I set the interval, I only set the property for the Axis Interval property
chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1;
The default behavior sets the major grid and major tick to this interval unless you have otherwise overridden a property that turns auto off for everything. Microsoft Charting aka Dundas charting (where Microsoft got the code) can be tricky this way.
While I cannot speak for every property within Microsoft Charting, it helps to set the most non-specific property you can find. Only go deeper if you do not get the desired result as going deeper can have unintended consequences by overriding the defaults which generally work quite well.
i.e.:
chart1.ChartAreas["ChartArea1"].AxisY.Interval = 1;
instead of
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 1;