Creating a chart in Excel, with sparse and different timelines in C# - c#

I'm trying to create charts in Excel.
The series(es) have different and sparse timeline.
Eg, let's say I measure temperature for different sensors/devices.
For device #1, I can have this data:
(indicating the time of measurement, and value)
9:30:12 = -100
9:45:17 = -90
and for device #2:
9:33:12 = -80
9:47:17 = -50
As you can see, the times are sparse/scattered.
My problem is that when I try to add these values as DateTime, on XValues,
the chart wrongly aligns all series to the same start time.
See attached image.
I've managed to do some temporary bypass, if instead of DateTime for XValues,
I use int of "milli-seconds-from-midnight",
as you can see in the attached image.
Then the line is drawn correctly, but it's hard to understand the scale/labels, and I want to fix it and show it as time/dates.
In the code, for chart type I use:
XlChartType.xlXYScatterLines
and for the values I uses:
newSeries.Values = values.ToArray();
newSeries.XValues = dates.ToArray();
Any idea how to fix it to work correctly with DateTime ?
Thanks for any help,
Si.

Related

Is "Pixel Value" to get Hounsfield value the IPixelData data resulting from the PixelDataFactory.Create using Fellow Oak Dicom Lib?

I could only find a reference explaining how to get the Hounsfield value using the Fellow Oak library which says it's determined by formula
Hounsfield units = (Rescale Slope * Pixel Value) + Rescale Intercept
Is "Pixel Value" in this formula above the IPixelData data resulting from the Create Method in PixelDataFactory?
For the MONOCHROME2 image dataset I'm working with the DicomPixelData is BitsAllocated=16 BitsDepth.BitStored=16. The PixelDataFactor.Create method returns GrayscalePixelData16S IPixelData and this data for all images ranges in values (using MinMax()) -2048 and 1013). I generate this IPixelData.Data with:
var dicomFile = DicomFile.Open(string.Concat(path, "/", file.Name));
DicomPixelData pixelData = DicomPixelData.Create(dicomFile.Dataset, false);
var pixelDataRender = PixelDataFactory.Create(pixelData, 0);
For the 95 axial images I'm working with, the Rescale Intercept=0 and Rescale Slope=1, so in my case Hounsfield unit would be the "Pixel Value" input in the formula shown in the stackoverflow post.
References I could find say Hounsfield value ranges -1024 HU to 3071 HU, does that mean the resulting IPixelData data is not the Hounsfield value? ultimately, how then can I get the Hounsfield field using the fellow oak lib?
In case it helps anyone, I think the GrayscalePixelData16S data was correct. In fact the images view correctly. I had not considered converting the 16 bit of data to select the data to display. So I changed the approach - instead of taking raw pixel data as hounsfield values, in the case of the data I'm using I applied default linear function for window center and width as a way to reach for hounsfield values, meaning I change window width/center to target houndsfield values.
If VOI LUT Function (0028,1056) is absent or has a value of LINEAR, Window Center (0028,1050) and Window Width (0028,1051) specify a linear conversion from stored pixel values (after any Modality LUT or Rescale Slope and Intercept specified in the IOD have been applied) to values to be displayed.

White Space in MS chart

I have a simple chart with 12 data points. The problem is that it shows a little white area before starting the trend lines.
Here is the code
for (int i = 0; i < 12; i++)
{
chart1.Series[0].Points.AddXY(DataTable.Rows[i].ItemArray[0], plotDataTable.Rows[i].ItemArray[1]);
chart1.Series[1].Points.AddXY(DataTable.Rows[i].ItemArray[0], plotDataTable.Rows[i].ItemArray[2]);
chart1.Series[2].Points.AddXY(DataTable.Rows[i].ItemArray[0], DataTable.Rows[i].ItemArray[3]);
chart1.Series[3].Points.AddXY(DataTable.Rows[i].ItemArray[0], DataTable.Rows[i].ItemArray[4]);
}
First column of DataTable is string and the other four are floats.
You need to set the AxisX.Minimum to a suitable value.
Usually this would be 0 or the x-value of the first DataPoint.
But the way you add the values this will not work.
You are adding the DataPoints in a rather unfortunte way, which sometimes is ok, but more often than not it will create all sorts of problems.
The recommended way is to add the x-values as numbers, or DateTimes, which internally will be converted to doubles.
But you add strings. This looks ok but the x-values contain neither those strings not anything else but 0s. Threfore you can't use them to set the range or tooltips or zoom ranges or to calculate stuff..
But if you want to you can still get the result you want by setting the minimum to 1:
ChartArea ca = yourChart.ChartAreas[0];
ca.AxisX.Minimum = 1;
I have added my x-values as string, too, but they look like numbers.
But the recommended way would be to convert your values to numbers so you can use them for all sorts of things..
A few notes:
This conversion is done by the chart, if at all possible for the y-values but not for the x-values! Maybe because a chart without numeric y-values makes no sense at all while sometimes x-values simply do not contain meaningful numeric data, like names, IDs, zip codes etc..
Don't let the visuals fool you: The strings are only copied into the axis labels; otherwise they are lost! (You should check this with the debugger!!)
You may notice that the number of Label changes in the screenshot. The number is calculated from the Interval of the x-axis. By default it is calculated automatically (Interval=double.NaN)to fit in a reasonable number. You can set it to any distance you like. Normaly it refers to the axis unit but in this case to the number of points. Set it to 2 to get one Label for every 2nd point; set it to 0.5 to get 2 Labels per DataPoint..
With real numbers (or DataTimes) as x-values you can also set a type for the interval like seconds or days..
By default, the ChartArea's IsStartedFromZero property is set true. This setting will force the chart to always start from zero. Try setting it to false:
chart1.ChartAreas[0].AxisX.IsStartedFromZero = false;
chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;

How to draw discontinued chart in C#

Now I'd like to draw a candlestick chart in C#. Problem I have is how to skip certain time of the x Axis. Say, the data starts from 9:00 and ends at 11:30. Then restart at 13:00 and ends in 15:00. If I fill data in it, period 11:30 to 13:00 will also be shown as a line. How to skip it and make it a consequence chart?
Set series.IsXValueIndexed to true, that should fix your problem
series.IsXValueIndexed = true;
All data points in a series use sequential indices, and if this
property is true then data points will be plotted sequentially,
regardless of their associated X-values. This means that there will be
no "missing" data points.
Take a look at documentation for more info.
I had same problem but after setting series.IsXValueIndexed = true, it not worked for me.
So I've found another way of achieving this.
series.XAxisType = AxisType.Secondary;
series.IsXValueIndexed = true;

Optimize Chart with 10000+ points

I have chart that can contain a lot of points (10000 +)
When I scale the chart in order to see all points in screen, it takes some time to draw them
Can You advice me some optimization, in order not to draw all points
I'm not an expert with listed technologies, but I would solve this by 'bucketing' your data points.
Your X axis is time, so determine the resolution point for the current chart size. IE, if you are seeing the entire chart you will only need a data point per day for example. If you are zoomed in a long way, you might want a point per hour.
Now you have determined resolution, go through your chart, and find all the data that exists between the resolution points, IE, all data that is > 20th April 2011 at 4pm and < 20th April 2011 at 5pm if you are on an hourly resolution.
Depending on the type of data you are using, will determine if you want to average all the data point you have collected, or find the median (or some other method, such as a candle stick chart to show the max/min values). Either way, pick the most relevant method, repeat for all points and render the result with your new data.
Hope that's what you meant.
Seems like you should use some sort of level of detail (LoD) algorithm.
For example:
Always use a maximum given set of points to represent all your actual points. By calculating local minima and maxima you can create a proper representation of the given point set for a certain 'detail', depending on how far you are zoomed in.
Calculating these extrema can still prove to be slow, so you might need to cache them. You can calculate and cache this on the fly as new data arrives.
In addition to the other good suggestions, I would
Do some random-pausing on it, to see if it's spending much time doing something else that could be avoided, such as maybe allocating new point structures all the time.
Rather than paint directly to the window, paint to a bitmap, and copy that to the window. It always looks faster, and sometimes it even is faster. (Be sure to stub out the method that clears the window background.)
I had experienced a severe performance problem with thousands of Series added to the chart rather than thousands of Points. The solution that worked for me was a flavor of the Flyweight pattern:
Instead of adding 1000-s of series, add just a single one.
At the end of a virtual series, i.e., when all points of the series have been added and it's time to move on to the next one, insert an empty point:
series.Points.Add(new DataPoint(0, 0) { IsEmpty = true });
Hope that helps somebody.

Auto-Interval precision in MS Chart

I'm currently using the charting within .NET using System.Windows.Forms.DataVisualization.Charting.Chart. Thus far it seems very powerful, and works great. However, there is a huge problem in terms of how it is auto-calculating intervals. I use a lot of double values, and in libraries like ZedGraph, it handles this perfectly. It selects min/max/interval just fine. However, in MS Chart, it may select 206.3334539832 as a minimum, and intervals of a similar decimal precision. Obviously this looks quite ugly.
So, I tried simply making the axis format {0.00} and it works great when it loads the chart. Except when you zoom in, you need greater precision, maybe at 4 decimal places instead of 2. It seems I'm either stuck with 9 decimal places all the time, or else a constant fixed number that may break when someone requires greater precision. I'd rather it pick up the precision based on the level of zoom currently applied. Libraries like ZedGraph and Dundas (which I believe MS is even using!) tend to pick good values that change as you zoom in and out.
Is there any way to have the intervals change precision as the zoom frame changes? It's probably some simple property I have set wrong, but it's hard to tell with the millions of properties this thing has (especially when there's about 14 places that represent the concept of Interval).
I had the exact same problem when zooming. I added code to format the axis labels and call it from the Paint handler. The Axis View objects have an IsZoomed property and have functions to get the current axis limits (GetViewMinimum/Maximum). I set the Axis LabelStyle.Format to "N" for all cases unless the Max-Min=range is less than 1. Then I set the format to "F#" where # is calculated based on the axis range.
# = Convert.ToInt32(Math.Abs(Math.Log10(range) - .5)) + 1;
Having played around with the chart control I haven't been able to find a simple solution to your problem. However the following may help:
Have you considered setting the maximum and minimum values for the axes yourself? If you round the actual maximum and minimum values to the nearest sensible "round" number (5, 10, 0.5, 0.01) this should make the calculated intervals a bit more friendly.
I understand this is not an ideal solution but by carefully choosing the maximum and/or minimum values you can ensure the intervals are "nicer" numbers. If the range of your axes is say divisible by 2, 5 & 10 it should result in fairly nice intervals.
Why not modify number format string.
Create format string
string formatString = "{0.00";
Identify zoom level, say zoomLevel = 2;
formatString = formatString.PadRight(5+zoomLevel, '0');
formatString += "}";
Now use this format on axis legend. Use string builder or some better way to modify the format string.
To provide the result with minimal cost you can use exponential scientific format
You can attach to customize event.
From there, you can modify the labels on x-axis:
var xAxisLabels = chart1.ChartAreas[0].AxisX.CustomLabels;
...
xAxisLabels[0].Text = ...
set min. and max. values:
chart1.ChartAreas[0].AxisX.Maximum = ...;
etc.
you can dynamically update the max and min based on your data set. each time user zooms in, you do a FOREACH on every point and get the stats and based on that set your max and min
It helps to set the IntervalOffset of the axis, here an example:
Private Sub chMap_AxisViewChanged(sender As System.Object, e As System.Windows.Forms.DataVisualization.Charting.ViewEventArgs) Handles chMap.AxisViewChanged
'the grid ticks are rounded values
e.Axis.IntervalOffset = -e.Axis.ScaleView.ViewMinimum
End Sub

Categories

Resources