For the last couple of days im trying to use MS-chart control to chart StockData (1 min interval, candlestock) which i have on my sql server. i thought its going to be easy... but now im at a point where im gonna cry soon.
im having so many problems if anyone can solve any he will be blesses for ever:
// my code is in the bottom, i may have done some fatal mistakeswhenever i change
series.XValueType = ChartValueType.Time;
the scroll bar doesnt work properly anymore, i cannot drag it anymore, i can however point somewhere on the scroll bar and it will take me there, but i cannot smoothly drag it as i could when it was plain int index.
where i scroll through the different hrs(minute by minute) im having serious problems
with Y-axis, i set the maximum and minimum on the begining but its not very effective as u scroll through the chart.for each point i set (in the populating loop)
series.Points[index]["PriceUpColor"] = "Green"; series.Points[index]["PriceDownColor"] = "Red";
but all the candles turned out red regardless.
i have a button when it clicked i want the chart to be populated then, this does not work, its only works if i put my code under form_load
i set the axis lbl
chartArea.AxisX.LabelStyle.Format = "hh:mm";
is this the correct code?
when i have float values (example:26.52), but when i put those values in the series.Points.AddXY, the result on the chars will be 26.5273287623 which of course doesnt help, i tried many diffrent solutions (casting, removing the digits my self etc) but couldnt find a solution
Lastly here is my code, i put a chart on my form and i call this function to change the chart setting and populate i t(am i already doing wrong?)
I realize that this is quite long, what can i say, Thank you for your help! (all help is welcome!)
private void MyChart(Chart chart1, DailyBarData dbb)
-> getting a chart component and dailybardata, and theoretically should populate the chart accordingly
-> iv insert some random data as well {
int viewSize = 30;
chart1.Series.Clear();
var series = chart1.Series.Add("Price");
series.ChartType = SeriesChartType.Candlestick; 开发者_Go百科
series.YValueType = ChartValueType.Auto;
series.XValueType = ChartValueType.Time;
DateTime date = dbb.Bars[0].time.AddMinutes (1); //getting the first minute
double viewdistanceDouble = date.AddMinutes(viewSize).ToOADate(); // getting the initital X loc - im guessing this is not good
double startdateDouble = date.ToOADate();
double maxy = 0; //minmax values for the chart
double miny = 10000000;
for (int index = 0; index < dbb.Count()-1; index++)
{
BarData b = dbb.Bars[index];
series.Points.AddXY(
b.time, // X value is a date
b.TwoDigits (b.High), // High Y value - is originally a float, here i mult by 100, cast to int then double then divide by 100
//and gets the exact same result..... i realize this is unneeded but i cant solve it
b.TwoDigits(b.Low), // Low Y value
b.TwoDigits(b.Open), // Open Y value
b.TwoDigits(b.Close)); // Close Y value
if (b.Low < miny) miny = b.Low;
if (b.High > maxy) maxy = b.High;
// u can use this code instead, however those values are ints and not floats which is half of the problem
// with this values i get less problems
//series.Points.AddXY(
// date, // X value is a date
// rnd.Next(40, 50), // High Y value
// rnd.Next(10, 20), // Low Y value
// rnd.Next(20, 40), // Open Y value
// rnd.Next(20, 40)); // Close Y value
//date = date.AddMinutes(1);
series.Points[index]["PriceUpColor"] = "Green"; // this code have unclear effects as it always red
series.Points[index]["PriceDownColor"] = "Red";
series.Points[index].Color = Color.Black;
series.Points[index].BorderColor = Color.Black;
}
var chartArea = chart1.ChartAreas[series.ChartArea];
chartArea.AxisX.LabelStyle.Format = "hh:mm";
chartArea.AxisX.Interval = 5;
chartArea.AxisX.IntervalType = DateTimeIntervalType.Minutes;
chartArea.AxisX.ScaleView.Zoom(startdateDouble, viewdistanceDouble); //starting scale , rarly works
chartArea.AxisX.ScaleView.SmallScrollSize = viewSize;
chartArea.AxisX.ScrollBar.IsPositionedInside = true;
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chartArea.AxisY.Maximum = maxy ; //by the values found on the loop, not good i want it to be automated when u scroll
chartArea.AxisY.Minimum = miny;
}
- This is known, but I don't know if it can be fixed either.
- Maybe you should give more details?
- Maybe you should show where you declare PriceUpColor and PriceDown.
- "series.Points.AddXY(b.time" is it a plain DateTime? have you tried using.ToOADate() instead?.
- I'd say it is correct.
- Maybe it is a precission issue because of casting, as the MS Chart controls work with doubles.
EDIT: For 1 you can try
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DataVisualization.Charting.DateTimeIntervalType.Milliseconds;
Or some other type.
精彩评论