How to specify fixed width to chart bars? - c#

I'm using Visual Studio 2015, working with WinForms.
I link 2 images to make you guys easier to understand what I want to do:
Example 1
Example 2
In the example 1 we have 10 values with an automatic width, this is the size I want always for bars, but this is a dynamic chart so when there are less than 10 values it just fills to graph as you can see in example 2.
I want the same size always, the size of the example 1, have tried to specify using:
chrt_ventesArticles.Series[Conversion.Texto(f_cursor.Campo(1))]["PixelPointWidth"] = "100";
And tried too:
chrt_ventesArticles.Series[Conversion.Texto(f_cursor.Campo(1))]["PixelPointWidth"] = Conversion.Texto(Math.Round(chrt_ventesArticles.ChartAreas[0].InnerPlotPosition.Height, 0));
But it isn't working as it should, depending on the number of values it haves a different size.
Any ideas about how to do it?

To space the bars evenly over a fixed number of slots you need to set the Minimum and Maximum values to display on an Axis.
Here you have a Bar chart and want to display the bars in a 10 slots.
So you write maybe:
yourCharArea.AxisX.Minimum = 0;
yourCharArea.AxisX.Maximum = 10;
Now you get both:
Always the same width of the Bars, even while some DataPoints are missing.
But also still the automatic scaling when you resize the Chart itself.
The latter will not work when you set the with of the Bars in pixels..!
Before and after:

Related

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..

How to adjust the label position in an Indexed X-axis on Chart

I am creating a chart in WinForms in C# and the X-axis is indexed.
I have 10000 points in the chart series
For some reason, the X-axis labels are labeled From 1 to 10001. Which means when I set the X-value (time) for any point, it does not get displayed for the 10001th label.
I tried setting the following parameters, none of which helped
chart1.ChartAreas[0].AxisX.MajorTickMark.IntervalOffset = -1;
chart1.ChartAreas[0].AxisX.LabelStyle.IntervalOffset = -1;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false;
None of this did anything to help, my chart labels are still reading from 1 to 10001, and no matter how much I zoom in (with added zoom controls) the labels still read on every interval ending with 1. Like 1001, 2001, 3001, .. etc.
How can I change this to read from 0 to 10000? And only show lines and labels on intervals ending with 0
Thanks
EDIT:
The minimum and the maximum were already set, in the screenshot you see below, all the X values are correctly indexed, but for some reason the chart display value 10001
And I do not wish to set the value to the Maximum to 10000 in this case, it will prevent the label from showing up but it will also prevent the ability to scroll the axis past the last measurement.

How can I manipulate Winforms chart

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!

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;

How do I add a tooltip or overlay to a specific section of a chart?

I have a chart that displays several lines showing signal strengths over a frequency band.
Each chart is composed of one 'area' and four 'series'. On the parent form there are several graphs like the one shown below. All of them are created dynamically and will have different widths.
What I am trying to do is add a tooltip or annotation (or something) when the mouse hovers over a specific area of the chart as shown in the mockup below:
If the mouse moved to the other side of the chart a different channel number and frequency would be shown in a box surrounding that area of the chart.
It doesn't have to be exactly as shown in the mockup although an outline would be preferred in order to show the user how wide the channel is regardless of the waveform shown in that area at the time. For example, the waveform shown above might only be 8MHz wide but channel 1 itself might have an allocation that is 10MHz wide (the device varies its bandwidth based on its offered load.)
The X axis is MHz and a channel is defined in terms of MHz so it would be ideal to define the outline in terms of the X axis instead of pixels.
Also, note that this is a realtime chart that is updated up to 10 times per second. Therefore it would be best if the information was not required to be updated each time new data arrived.
I was able to combine a couple of items to make the following solution:
[credit LICEcap]
The highlight is a rectangle filled in the 'OnPaint' method of the chart control.
The text is a simple TextAnnotation that is applied during the mousemove event.
It took quite a bit of coordinate conversion to get all the pieces in the right spot - especially the text. I needed to convert between pixels, position and value.
The first conversion was to pixels in order to center the text using MeasureString. I then converted it from pixel location to X axis value and then needed to convert it to position since the annotation requires using 'position' coordinates. There is not a function to convert from pixels to position. There is a pixels to value and a value to position which is the way I went.
I don't claim this to be the best or even a proper way to do it but it works. If anyone else has a better solution or a way to improve my code please post.
Here's my code for positioning the text:
double temp = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(Convert.ToDouble(ce.sChannelFrequency) * 1000);
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1))) {
SizeF size = graphics.MeasureString(freq.Text, new Font("eurostile", 13, FontStyle.Bold, GraphicsUnit.Pixel));
temp -= (size.Width/2+10);
}
if (temp < 0) temp = 0;
temp = chart1.ChartAreas[0].AxisX.PixelPositionToValue(temp);
freq.X = chart1.ChartAreas[0].AxisX.ValueToPosition(temp);

Categories

Resources