I have an array of type Double() (1 x n) that I am trying to quickly plot on a graph I've already set up. The only thing I want/need to do is take my array and store it as data points (y-axis values) in the series I already have. What's the best way to do this? Also, will the data points that are plotted change as my array changes, and if not, how would I replot those new points and get rid of the old points?
Adding the points should be as simple as
Chart1.Series("Default").Points.Add(arrayName)
As for it being auto-updated when you change the array, I believe if you just add the points, you are going to have an issue like pee in a swimming pool (once they are in there, you can't get them out). So, you add an array with 3 items, then add a double to the array, then the add to the chart again, you now incorrectly have 7 points. However, you can databind the array to the series, like so.
Chart1.Series("Default").Points.DataBindXY(xStrings, xDoubles)
In this case, if the array changes, the chart should change as well.
Related
VB.net - How come only one point is plotted?
Dim ReceivedValue As String ="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"
Dim myArray = Array.ConvertAll(ReceivedValue.Split(","c), AddressOf Convert.ToDouble)
Chart1.Series(0).Points.Add(myArray)
PS. I got the plot array idea from here
I get the same results as you - one point on the chart. Upon inspecting the chart's Points object, it looks like this:
Showing an array with one X=0, and an array of all the Ys.
If you do it in a loop like this, it works
For Each point In myArray
Chart1.Series(0).Points.Add(point)
Next
Now there is an actual series of points
I'm going to add what I think is more correct, because this results in actual x, y pairs with real x values (you can make them whatever you want)
For i = 0 To myArray.Count - 1
Chart1.Series(0).Points.Add(New DataPoint(i, myArray(i)))
Next
I'm working on a small project, and one of the components utilizes an animated dice. I've got the mechanics of the dice down, however, I wish to make the dice colour change each time it rolls.
As of now, I have to manually set the colour
(e.g. pnl1.Backcolor = system.Drawing.color.Red).
I've already set up an array with the various different colours and intend to reference them randomly using the random number function, but my question is how can I refer to an item in the array in such a way that that makes the above mentioned pnl1.Backcolor match said colour?
I'm well aware I can't just use system.Drawing.color.Colours(1), so how might I go about this/what are some possible alternate options to an array?
Any and all help is massively appreciated and I do apologise if the way I've formatted this question is not in line with that which the website demands (I'm relatively new).
Thanks,
~ John
I'm not sure whether you have a different panel for each side of the die or just one with a picture change, nevertheless, below is an example of something you could do. Change as needed (I'm assuming just one panel with color change - pnl1).
Dim PanColor() As Color = {Color.White, Color.Red, Color.Green, Color.Blue, Color.Purple, Color.Yellow}
pnl1.BackColor = PanColor(put_random_number_here_0_to_5)
I'm not sure this is possible but thought this was the best place to ask.
Is it posible to get the position of a series value on a graph in excel?
For example, if I have a line graph in excel that has time along the x axis, is it possible to (using VBA) get the position of a specific point on that axis.
What I am trying to do is have a vertical line that is can be positioned based on a date entered by the user.
like this
Where the green line could be positioned by entering in a date (rather than just being manually moved) (or also it could be set to automatically move to the current date etc).
I was then thinking that if the position is on the graph is queryable, then I can just access the line object and move it to any position I wanted through VBA.
Any Ideas? or is this just not possible?
The "cleanest" way to do this is to add the line to the chart as a new series. In that way, Excel handles all of the positioning and your work is simplified. To get a vertical line on a chart, there are a number of options. I prefer this route:
Create a small 2x2 area with two dates and two values
Add in the date or x-axis value you want the line at (E3 in image). You can use =TODAY() here or some manually entered value.
Set the second x-axis value equal to the first
Use MAX and MIN on the data to get the values for each date. You can also use 0 and 1 and a secondary axis, but I think MAX/MIN is easier.
Add the data to the chart and format as a marker with straight line.
Formulas
E3: =TODAY()
E4: =E3
F3: =MIN(C3:C27)
F4: =MAX(C3:C27)
Result and chart data series for vertical line
VB 2008
.NET FRAMEWORK 3.5
MSCHART - FASTLINE CHART TYPE
Is it possible to have an MS Chart control contain 20,000 data points, but only show the last 100?
I know that I can select the last 100 from my datatable and use it as a datasource.
Chart1.DataSource = cMs2.dsData.Tables("readings").Select(wFilter, wSort).Take(100)
That's not what I want.
I know that I can populate an array or collection with the last 100 data points and use it as a data source.
Chart1.Series("readings").Points.DataBindXY(colCtr, colReadings)
That's not what I want.
I need to do 1 of 2 things:
Manually add data points and be able to show only the last 100 or last 1000 of them that just came in. This must be done without re-populating the chart. Just show a portion of the complete set of data points.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
Chart1.Series("readings").SHOWONLYTHELAST100DATAPOINTSWITHOUTCLEARING
Initialize a chart with a certain number of the total readings with a databind, then manually add new data points one at a time while removing the oldest data point. For example, initialize the chart with 100 data points, then add a new data point, remove the first data point, getting us back to 100. (I'm successfully doing this one, except the chart doesn't behave like I expect. The chart grows, remaining blank/empty where the 'removed' data points were. I do chart.update but it doesn't refresh it.) Note that I am allowed to take more time to initialize the chart (clear/populate), I don't have that time to do it as each new data point comes in.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
If Chart1.Series("readings").Points.Count > 100 Then
Chart1.Series("readings").Points.RemoveAt(0)
Chart1.Update()
End If
NOTE: Doing a process that causes me to have to clear and rebind the data to handle the addition of a single data point causes me problems because it takes too long. I'm just looking for the quickest, most efficient way to handle this. Thank you for taking the time to read this...!
You can manually set the Minimum and Maximum values of each axis by modifying the .Minimum and .Maximum values of the axes like
Chart1.ChartAreas(0).AxisX.Minimum = 100 'Example value
Chart1.ChartAreas(0).AxisX.Minimum = 200 'Example value
As discussed in the comments to your question you can either use your method #2 (add datapoint and delete datapoint index 0 and then select the new minimum and maximum X-Value from the series with LinQ:
Chart1.ChartAreas(0).AxisX.Minimum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Min
Chart1.ChartAreas(0).AxisX.Maximum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Max
or you can, as you have now done, just set the minimum value to a X-value some points before the last point.
I am using Microsoft.Office.Interop.Excel to create a chart. Some of my series in chart series collection are logarithmic, so I want to get hold of axis related a particular series, to set scale type to logarithmic.
Here is a way you could do it:
xlAxisValue = CType(xlChart.Axes(, Excel.XlAxisGroup.xlPrimary), Excel.Axes)
xlAxisValue.Item(Excel.XlAxisType.xlValue).ScaleType = Excel.XlScaleType.xlScaleLogarithmic
It is a generic example, if it is not working plese post your code.