I have a simple line series chart using WPFToolKit. I would like to have new data coming in to be updated on the chart every 5 minutes. But I have no idea how to go about it. The way I am showing data now is this.
public Window1()
{
InitializeComponent();
showColumnChart();
}
private void showColumnChart()
{
List<KeyValuePair<double,double>> Power = new List<KeyValuePair<double, double>>();
Power.Add(new KeyValuePair<double, double>(30, 40));
Power.Add(new KeyValuePair<double,double>(50, 60));
//Setting data for line chart
lineChart.DataContext = Power;
}
Also, do I have to use a database? Any help would be appreciated.
Thanks,
Check http://www.c-sharpcorner.com/UploadFile/mahesh/WPfTimer09292009090518AM/WPfTimer.aspx (using Dispatcher Timer, you can achieve that).
Related
So I'm having a really weird problem in C# WindForm. I've tried to create a chart. In the area where the chart itself should exist, the problem is that it displays no data. It just looks like that:
That's my code:
int questionsAmount = setData.questionsAmount;
var dates = new List<DateTime>();
foreach(var item in setData.dates)
{
dates.Add(Convert.ToDateTime(item));
}
int[] records = setData["records"].ToObject<int[]>();
Series series = new Series("records");
series.Points.DataBindXY(dates, records);
series.ChartType = SeriesChartType.Spline;
chart = new Chart();
chart.Name = set.title;
chart.Series.Add(series);
chart.Series["records"].SetDefault(true);
chart.Series["records"].Enabled = true;
chart.BackColor = SystemColors.Highlight;
Controls.Add(chart);
Refresh();
Any help would be much appreciated. Thanks!
Hmm.. First, because we cannot read the entire code, I cannot sure the data generating process is working well. As your picture, the chart control is added. Maybe you can add breakpoint on "series.Points.DataBindXY(dates, records);" and check whether the data is ready or not.
FYI, another probable issue is you need to do UI operation in main thread (UI thread) if you didn't.
Hope these can help you.
I am currently using WinForms. I have two Lists of Values (doubles) which I visualize with the NuGet package "LiveCharts". Now to my problem: The list of the values constantly changes as new values are added to both lists with a frequency of up 200 values / second. Right now I am doing this with Random Values. In a loop I add a new random value to each list and remove the value with the index 0. Now I want the chart to update everytime a new value is added to the list, but although I call the method which should do that everytime I change the values, the chart only updates after the loop ended.
This method is called by a button and should add 10 new values. This works, but the chart only updates after the for-loop is finished, although the UpdateChart() method is called everytime.
private void ReloadButton_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
valueList1.RemoveAt(0);
AddRandomValueToList1();
valueList2.RemoveAt(0);
AddRandomValueToList2();
UpdateChart();
}
}
This is the UpdateChart() method I use to update the chart Revenue is a class object and contains a value and a DateTime
private void UpdateChart()
{
series1 = new SeriesCollection();
// series.Clear();
double[] values1 = new double[valueList1.Count];
double[] values2 = new double[valueList2.Count];
int index = 0;
foreach(Revenue current in valueList1)
{
values1[index] = current.value;
index++;
}
index = 0;
foreach (Revenue current in valueList2)
{
values2[index] = current.value;
index++;
}
series1.Add(new LineSeries()
{
Title = "Values1",
Values = new ChartValues<double>(values1)
});
series1.Add(new LineSeries()
{
Title = "Values2",
Values = new ChartValues<double>(values2)
});
cartesianChart1.Series = series1;
}
I have already tried to add breaks (Thread.Sleep) hoping the chart would refresh, but that did not work. As well as the cartesianChart1.Refresh() method which I called in the for-loop. Also did I disable the animations, but still got not the result I was hoping achieve.
Now I do not know what I can do to force the chart to refresh everytime I change the values of the lists in order to create a realtime chart.
I am grateful for every help
Thanks in advance
I am trying to use live chart for wpf c#. Generally I got it to work to display a graph. However when i try to call the function below more than once it does not execute the second time. Why? If I restart the program it works but only the first time again. Anybody familiar with live charts (lvcharts) library and can give me some direction. Thank you!
public void FillTheGraph(string[] axisX, double[] axisY)
{
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Graph",
Values = new ChartValues<double>(axisY)
},
};
Labels = axisX;
DataContext = this;
}
Problem
i've run into an issue where deserializing any value with json.net (even dummy values) seems to mess up the dates along the x-axis of my mschart control. this is a chart that's supposed to have date values along the x-axis:
===bad===
the chart is produced by the following minimal code:
using System.Windows.Forms.DataVisualization.Charting;
using System.Threading;
using Newtonsoft.Json;
public partial class Form1 : Form
{
Thread thread;
public Form1()
{
InitializeComponent();
chart1.Dock = DockStyle.Fill;
thread = new Thread(Plot);
thread.Start();
}
void Plot()
{
// prepare chart
chart1.Invoke((MethodInvoker)delegate
{
chart1.ChartAreas.Clear();
chart1.Series.Clear();
ChartArea area = new ChartArea();
chart1.ChartAreas.Add(area);
Series series = new Series("water level");
series.ChartArea = area.Name;
series.ChartType = SeriesChartType.Line;
chart1.Series.Add(series);
});
// plot line by adding 2 points: ((time)0, 0) and ((time)1, 1)
for (int i = 0; i < 2; i++)
{
object dummy = JsonConvert.DeserializeObject<object>("null");
chart1.Invoke((MethodInvoker)delegate
{
DateTime time = new DateTime(1970, 1, 1, 0, 0, i, DateTimeKind.Utc);
chart1.Series["water level"].Points.AddXY(time, i);
});
}
}
}
commenting out the only json.net line following line results in the expected result:
object dummy = JsonConvert.DeserializeObject<object>("null");
===good===
Question
is there something wrong with my code?
Update
i just replaced the concerned line with
Thread.Sleep(5000);
and the problem still occurs. i thought that maybe i'm modifying the chart too early. but now, i'm not sure what to make of it.
i had a look through the properties of the Series object, and saw that XValueType was set to "Double". so i tried explicitly setting it to "DateTime" when creating it, and that solved the problem:
Series series = new Series("water level");
series.ChartArea = area.Name;
series.XValueType = ChartValueType.DateTime; /* <---- added this line */
series.ChartType = SeriesChartType.Line;
chart1.Series.Add(series);
it is set to "Auto" by default, which appareantly does not always get set to "DateTime". i guess i should have looked at the properties much earlier.
it's still a strange issue to me, and i have no idea what causes it, so this is more of a workaround.
If I have a candle series drawn on the teechart, and I change the last candle's CloseValue, the candle does not automatically update on the chart. I don't really want to invalidate the whole chart to show it because if there are a lot of candles, that's slow. I thought the DrawValue method of the Candle series would solve this, but it seems that it actually redraws the whole chart as well. Is there a way to update/redraw on screen just that one candle?
I know when I was doing this in Delphi 5 the candle seemed to update without redrawing the whole chart - although maybe the whole chart updates were fast enough that it just seemed that way. In general, it seems that C#'s Teechart draws are 3 to 5 times slower than the Delphi 5 VCL Teechart's draws...
I think you can use the method that allow refresh only the series, RefreshSeries, as do in next line of code:
candle1.RefreshSeries();
Could you tell me, if it works for you? If you have any problems please let me know.
Update information to answer next question:
Ok, I guess I am not explaining myself well here, Sandra. Let me try
again. Let's say I have a chart that has only one series - 30,000
candles. Let's say I show ALL candles on the chart. Redrawing all 30K
candles takes time. Let's say the code changes only ONE candle - the
last one. Is there any way to just repaint that little portion of the
chart that has the new candle, and not repaint the whole chart in
order to show the new change? I tried Invalidate function passing it
the candle's rectangle, but it seems that Invalidate() and
Invalidate(rect) produce exactly same results.
Can you tell us which version of TeeChart are you using?. In the other hand, I have made a simple code where I have modified the CloseValue and I have released that isn't necessary repaint, redraw or refresh Series because the value is updated automatically. I have made the test using last version of TeeChartFor.Net Build number [4.1.2012.01312] and next code:
Steema.TeeChart.Styles.Candle candleSeries1;
Random r;
double tmpOpen;
double tmpClose;
int count;
DateTime dt;
TimeSpan ts;
private void InitializeChart()
{
tChart1.Aspect.View3D=false;
tChart1.AutoRepaint = false;
r = new Random();
candleSeries1.Clear();
candleSeries1.XValues.DateTime = true;
candleSeries1.GetHorizAxis.Labels.Angle = 90;
count = 0;
dt = DateTime.Today;
ts = TimeSpan.FromDays(1);
candleSeries1.Pen.Visible = false;
for (int t=0;t<30000;t++)
{
tmpOpen = r.Next(100);
tmpClose = tmpOpen - r.Next(100);
++count;
candleSeries1.Add(dt,tmpOpen,tmpOpen + r.Next(50),
tmpClose -r.Next(50),tmpClose);
dt += ts;
}
tChart1.AutoRepaint = true;
}
private void button1_Click(object sender, EventArgs e)
{
tmpOpen = r.Next(100);
tmpClose = tmpOpen - r.Next(100);
candleSeries1[candleSeries1.LastVisibleIndex].Close = tmpOpen;
}
Thanks,