Zedgraph labels disappears - c#

I have made bar diagram with horizontal lines, it works fine, and a combobox where the users can change to a pie chart, with sort of work, when the user change to pie chart it works fine, but when change back from the pie chart to the bar chart, only the bars come, not the title, the x-asis tick, they axis tick nor the custom labels for the y axis.
I have search the net, and tried the following lines
myPane.YAxis.Scale.IsVisible=true;
myPane.XAxis.Scale.IsVisible=true;
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 30;
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 9;
myPane.Legend.IsVisible = true;
but it only shows the bars there and nothing else. it works fine, if the bars diagram is made before, and without shift to the pie-chart

Found a solution, finally.
Making a rectangle for prooper size
RectangleF rc=new RectangleF();
rc.X=0;
rc.Y=0;
rc.Width=zgc.Width;
rc.Height=zgc.Height;
Make a new graph rather than a new masterpane
zgc.GraphPane=new GraphPane(rc,"title","x - axis","y-axis");
Link the new pane
myPane=zgc.GraphPane;
That worked for me, now I get the corect size, and all values and ticks.

Related

Control visual styles of individual gridlines

I have a c# RangeBar chart with about 25 labels on the AxisX. Depending on my live data, some of these labels have visible data points in the chart. Typically about 3/4 of the 25 labels don't have any data points. All horizontal gridlines are enabled, which makes the chart a bit cluttered. Especially for data points that are further away on the AxisY, it's difficult to distinguish at a glance to what label on the AxisX they belong.
My idea is to differentiate the AxisX labels that have visible data points either by giving their gridline a different color, a different style, or to completely remove gridlines of AxisX labels that have no corresponding data points. Is this even possible? Because all controllable properties and documentation I have found so far is regarding gridline styles of the entire chart.
Btw: Only showing AxisX labels that have data points is not an option.
Thanks in advance.
To reduce clutter you could use the axis.Interval property.
But each line will still have the same style, either of major or of the minor lines.
You could add VerticalLineAnnotations for selected points or try StripLines. Both can be styled freely.
Here is an example of a normal line chart, with lines randomly added to a few points:
The same result can be achieved by either Annotations or Striplines. The blue lines are StripLine and the red ones Annotations. Here are two functions to add a colored line to a DataPoint:
void AddStripLine(Chart chart, ChartArea ca, DataPoint dp, Color col)
{
Axis ax = ca.AxisX;
StripLine sl = new StripLine();
sl.BorderColor = col;
sl.IntervalOffset = dp.XValue;
sl.Interval = 0;
sl.StripWidth = 0;
sl.BorderWidth = 1;
ax.StripLines.Add(sl);
}
If you look very closely you'll see that the only visual difference is that the former are below, the latter above all other chart elements.
void AddAnnotation(Chart chart, ChartArea ca, DataPoint dp, Color col)
{
Axis ay = ca.AxisY;
VerticalLineAnnotation vl = new VerticalLineAnnotation();
vl.LineColor = col;
vl.AnchorDataPoint = dp;
vl.IsInfinitive = true;
vl.LineWidth = 1;
vl.Height = ay.Maximum - ay.Minimum;
vl.ClipToChartArea = ca.Name;
chart.Annotations.Add(vl);
}
For Bar charts use HorizontalLineAnnotations and anchor to the correct axis...
Using Annotatons is in fact the more natural way, since each is an individual object. StripLines are too, but are repeated at intervals . To avoid visible repetiton each needs to have suitable offsets and intervals to be determined for each. But it isn't hard to do either way.
Also note that the both types of lines lines are anchored to their DataPoints and will move with them e.g. when chart is resized..

Chart is rescaling area for axis labels when long labels appear due to scrolling

I have some string labels that are associated with a number each.
I've created a Chart element with one ChartArea and one Series which is using the SeriesChartType.Bar type to show my labels on the x-axis (which, confusingly, is the vertical axis when using the Bar type, but anyway...) and show the number as a bar next to it.
Since I have many labels that don't fit on the screen at once, I "enabled" the scrollbar using the Zoom method in my Paint event handler like this:
private void chart1_Paint(object sender, PaintEventArgs e)
{
var scaleView = chart1.ChartAreas.First().AxisX.ScaleView;
var pos = double.IsNaN(scaleView.Position) ? 0.0 : scaleView.Position;
scaleView.Zoom(pos, pos + chart1.Height / 22.0);
}
I don't know if this is the proper way to do that, but it does (almost) what I want:
show the scrollbar if there are too many data points (labels) to fit on the screen
update the visible area properly when the window is resized
There is only one annoying thing: If due to scrolling a long label appears in the visible area or disappears, the area occupied by the labels is adjusted to the longest visible label. I hope these screenshots explain what I mean:
Here one long label is visible (at the bottom):
Here I scrolled up by one unit so that the long label is not visible any more:
This is super annoying during scrolling as everything gets rescaled whenver a long label appears or disappears.
How to fix the area occupied by the labels to always fit the longest label in the series, no matter if it is currently visible or not? I tried IsLabelAutoFit = false for both, x- and y-axis, but that doesn't help.
Ok, I've got it. I used
chartArea.InnerPlotPosition.Auto = false;
InnerPlotPosition.X = 33.333;
to give one third of the chart area to the labels and the other two thirds to the bars.
InnerPlotPosition.Auto = false makes this fixed so that it doesn't update while scrolling.

Chart control width with many series

I have this chart with some series in legend and this chart is docking fill in panel. The legend has these properties:
chart1.Legends.Add(seriesName);
chart1.Legends[0].Docking = Docking.Bottom;
chart1.Legends[0].TableStyle = LegendTableStyle.Wide;
chart1.Legends[0].BorderColor = Color.CornflowerBlue;
chart1.Legends[0].BorderDashStyle = ChartDashStyle.Dash;
chart1.Legends[0].BorderWidth = 1;
Chart with only a few series:
With even more series but the same interval date I have this result:
The issue is not the scale of the data but the reduced size of the ChartArea itself.. - How can I fix this?
The reason is in this line:
chart1.Legends.Add(seriesName);
What it does is adding one totally empty Legend, most likely for each of the Series you add. It is placed at the default position, that is to the right. And if you get enough of them they push the ChartArea back to the left..
Simply remove the line, as all Series get added to the default Legend anyway. It is only that default Legend your next lines style and position docked to the bottom.
To demostrate the effect you can add a LegendItem to each:
Legend l = chart1.Legends.Add(chart1.Legends.Count + ""));
l.CustomItems.Add(Color.HotPink, chart1.Legends.Count + " *");
Result:
As you can see even very few extra Legends push the ChartArea way to the left. Yours are empty, so they don't take as musch room, but still..

Dotnetcharting stacked bar chart displaying like regular bar chart

I have a dotnetcharting chart that I want to display as a Stacked Bar chart, but when it actually displays it shows up as a regular bar chart with the bars next to each other.
I have Chart.YAxis.Scale = Scale.Stacked; and I checked and it is getting set properly. Does anyone know why it isn't working properly?
I found that if you create a new Axis for a Series you have to change the Series's Axis and not the Chart's Axis. So what I needed to do was: timeLossStackedBarChart.SeriesCollection[0].YAxis.Scale = Scale.Stacked;

c# Chart Label Positioning

I am trying to move the position of a label that i am setting at a specific point on my chart line to explain what the line is and what the value used was to create the line. The problem i am encountering is that the label goes right through the line. Is there a way that i can Move the label to the right to keep it from crossing lines.
Here is how i am setting the Label
chart1.Series["ZAV"].Points[average + (average / 2)].Label =
string.Format("ZaV = {0:n2}", zavSG);
Work Around
Although this doesn't exactly answer my original question that I posted, this is what I did. I made a new Series added one point at the location I wanted the label, in my case the very top right of my graph, and set the label to that point. In the chart properties I set the chart series properties for SmartLabelStyle to not allow the label outside the plot area.
double maxPointX = chart1.ChartAreas[0].AxisX.Maximum;
double maxPointY = chart1.ChartAreas[0].AxisY.Maximum;
chart1.Series["Series6"].Points.AddXY(maxPointX, maxPointY);
chart1.Series["Series6"].Points[0].Label = string.Format("ZaV = {0:n2}", zavSG);
chart1.Series["Series6"].SmartLabelStyle.Enabled = true;

Categories