How can I manipulate Winforms chart - c#

I have a problem fitting my chart:
How can I adjust or manipulate the y-axis numbers to start from where I want it?
Secondly, is there a way (properties) which may help me display this graph friendly? As we can see all x-axis name aren't displayed and maybe a space b/w grey and blue bars?
Any help to how to display charts friendly is welcome

You can set the Interval of each Axis:
yourChart.ChartAreas[0].AxisY.Interval = 17;
And you can make it start at any Offset:
yourChart.ChartAreas[0].AxisY.IntervalOffset = 11;
As for the X-Axis Labels it is hard to tell without knowing your code..
..but basically the same options apply here too:
yourChart.ChartAreas[0].AxisX.Interval = 1;
To control the widths of the columns you set their custom property PixelPointWidth but adding a gap is not possible.
All solutions I found are hacks and the best one is to add a dummy Series between the real data series:
chart1.Series[0]["PixelPointWidth"] = "33";
chart1.Series["gap"]["PixelPointWidth"] = "33";
chart1.Series[2]["PixelPointWidth"] = "33";
Fill it with the same number of DataPoints with an X-Value of 0!

Related

Align multiple charts

I have seen this answer, but what happens if you have three charts? And in general, have n charts stacked one ontop of each other and you want the chart bodies (areas) to align with each other?
I would be satisfied to see a result for three charts, but a function that takes a list of charts and aligns them is the most useful.
Also, does this answer presume the charts already have all data in them? What if the data is added at runtime dynamically and you need to keep the charts aligned? The problem is the y-axis labels may change in size as new data appears ( a negative sign appears, or more decimal places, more digits, etc), pushing the chart body to the right, and therefore misligning them with other chart areas stacked above/below it.
Being able to assign a stable Y-axis label extent no matter how big the label gets goes along way to solving some of these problems. How is this done?
This should work, assuming charts is an array of all the charts -
var referenceR = charts
.OrderBy(c => c.ChartAreas[0].InnerPlotPosition.ToRectangleF().Width)
.FirstOrDefault();
foreach(var chart in charts)
{
chart.Left = referenceR.Left;
chart.Size = referenceR.Size;
var r = chart.ChartAreas[0].InnerPlotPosition.ToRectangleF();
chart.ChartAreas[0].InnerPlotPosition = new ElementPosition(referenceR.Left, r.Top, referenceR.Width, r.Height);
}
This solution worked. I realize it is not what I asked, but for now this seems to be the internal design of the chart control "promoted" for solving this sort of problem.
Instead of having three charts, I have one chart with three areas. Then solving the alignment issue is trivial:
chart1.ChartAreas["ChartArea2"].AlignWithChartArea = "ChartArea1";
chart1.ChartAreas["ChartArea3"].AlignWithChartArea = "ChartArea1";
And everything stays aligned beautifully as data is inserted in realtime.

Show only values with more than 0 in chart

I have a chart who I pupulate like this:
hardSpecChart.DataSource = hardSpecModel;
hardSpecChart.Series.First().XValueMember = "TasksName";
hardSpecChart.Series.First().YValueMembers = "Time";
Problem is somethimes my Y values come with 0, so I don't want to show label of that items in chart, so to fix it I just do
hardSpecChart.DataSource = hardSpecModel.Where(x => x.Time > 0);
It works, but no as spected, I want to dissapear only labels from chart but not value at the top left of chart:
I want to show all labels at the top right of chart but I don't want to show at the graphic if they have value 0
For a Pie chart some extra rules apply:
Only the Y-values matter. (But if you add X-Values you can still refer to them in expressions, see below..)
You can have several Series but only Points from the 1st one show in the chart. Those from additional series do show in the Legend, though.
By default the Legend shows each Y-Value, not the Series name,
As usual the Legend can be influenced by properties LegendText and IsVisibleInLegend, both of which can be applied to either the whole Series or individual DataPoints.
DataPoints are shown as slices with a width (angle) proportional to their values compared to the sum of all absolute values; see the image below! This implies that points with a value of 0 do not show at all.
But: If you set IsValueShownAsLabel to true, zero-value labels still will show as 0.
To suppress those you can either
change that property for points that are 0 by looping over the points (ugly) or..
set a LabelFormat that suppresses the 0 for the whole series: someseries.LabelFormat = "#";
In the chart below I have set
chart1.Series[0].LabelFormat = "#";
chart1.Series[0].LegendText = "#INDEX. = #VAL";
There are many other keywords, which can also be combined..

c# - create Horiz. scrollbar on chart when data is plotted off-screen

I have a line chart, which, after enough data points have been plotted to it, the data will exceed what is visible on screen (so that the chart is only showing the most recent data). When this occurs, I would like a scroll bar to be filled for the X axis, allowing the user to use the scroll bar to view such previous data.
How do I go about doing this? I don't want the user to be able to drag or zoom on the chart itself, just to solely use the scroll bar to navigate along the chart.
I've looked at this article: https://msdn.microsoft.com/en-us/library/dd456730.aspx but it doesn't help & the scrollbars do not appear.
Without seeing the relevant parts of your code it is hard to pin down your problems.
Here is one strange statement:
after enough data points have been plotted to it, the data will exceed
what is visible on screen (so that the chart is only showing the most recent data).
Now this can only happen after you have set the AxisX.Maximum because by default the chart control will squeeze the area more and more while you add points.
But when you have set a maximum of what can be shown, no scrollbar can work or even been shown. Sounds logical, right?
So either don't set it in the first place or clear it when the number of points exceeds what you want to show. To clear it use NaN :
chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;
Or, of course, set it to the last point you want to be shown!
After looking at what you mustn't do let's see what you need to do to show the scrollbar:
First you enable it:
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
Next you tell it to show only the scrolling handle and not the zoom-reset button:
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
See MSDN on ScrollBarButtonStyles for the various things the scrollbar can show/do!
And to make sure the user can't zoom set this:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
And finally set the current range to show:
chart1.ChartAreas[0].AxisX.ScaleView.Size = 111; // show 111 DataPoints
Now the scrollbar should show.
It is a good idea to study the AxisScaleView class as it has a couple of helpful properties..
Depending on the data type of your X-Values you may also need to set the ScaleView.MinSizeType to whatever suits your data:
chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Number;

C# Calculate the image scroll with scrollbar

I'm working a 2d tile-editor, used Winforms and XNA(for the map rendering) and i have a problem with the custom scrollbar.
I would like to know the formula for calculating the map scrolling depending the position value of my scrollbar.
Let me explain
Actually, i have a functional code but is not exact.
Here is my calculation (is the problem):
(Point Map.PixelMapSize = map size in pixel)
hScrollBarMap.Minimum = 0;
hScrollBarMap.Maximum = 100;
hScrollBarMap.LargeChange = hScrollBarMap.Size.Width * 100 /(Map.PixelMapSize.X);
mapScrollCalcul = ((hScrollBarMap.Value - hScrollBarMap.Minimum) * 100 / (hScrollBarMap.Maximum - hScrollBarMap.Minimum)) * Map.PixelMapSize.X / 100;
Sorry, I do not know how to explain. I found it by making a lot of tests...
But, the mapScrollCalcul is the final calcul to be applied to map position display.
What is the exact calculation for this ?
I hope you understand, I'm not speak English, just little understand.
But I understand you. :) (programmation language is universal)
Thank you for reading and, maybe, future responses.
You are overcomplicating things.
First, you are setting your LargeChange value relative to your image, not your view, and your scroll.Maximum relative to... what, exactly?
On MSDN, they say:
User interface guidelines suggest that the SmallChange and LargeChange
properties are set relative to the size of the view that the user
sees, not to the total size including the unseen part. For example, if
you have a picture box with scroll bars displaying a large image, the
SmallChange and LargeChange properties should be set relative to the
size of the picture box, not to the size of the image.
Now, lets assume that your image is 1000x1000 and your view is 100x100, and you are scrolling vertically. Then:
myVscroll.Minimum=0;
myVscroll.Maximum=image.Height; //1000
myVscroll.SmallChange = 10; // here: view.Height/10 or other value that makes scrolling look smooth
myVscroll.LargeChange = 100; // in this example, this is set to view.Height
And finally:
mapScrollCalcul = myVscroll.Value;

ASP.NET Chart Manipulation with c#

To explain my question I drew an image, here it is:
So I have a Chart that has a bar graph and a line chart, 2 in 1. I am trying to displaly it in asp.net just the way i drew it on here. This means the A box on the image has a set of values which reflect the line graph, and C box on the image has a set of values that define the bar graph values.
What I want to ask is how do I set two different sets of values on a single chart. I know I can use series to draw multiple graphs and by having series defined as different types I can change if its a bar graph or a ilne chart, however I don't know how to add 2 range of values on the side.
As for the the box B, what I wish to ask is how do I set these values so that they are showing different dates, I was able to do it using a line of code shown bellow, however that line of code sets every value to same, and I need them to reflect different values such as range of dates in this example on the image.
Code that I tried is: Chart1.Series["Series1"].AxisLabel = "Test";
Help is most appreciated!
Cheers!
Ok so i'm going to leave this question up for those that come into similar problem.
This is how you set column A and column c
Chart1.Series[0].YAxisType = AxisType.Primary;
Chart1.Series[1].YAxisType = AxisType.Secondary;
Chart1.ChartAreas[0].AxisY.Maximum = 500;
Chart1.ChartAreas[0].AxisY2.Maximum = 7000;
Your grid lines might get messed up so if you want to disable them use
Chart1.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
And just as a bonus if you want to set intervals on each Y axis you can do that with
Chart1.ChartAreas[0].AxisY.Interval = 50;
I still haven't figured out how to do part B that I mentioned, so if you know the answer then please share.
Cheers!

Categories

Resources