SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
Can you help me please!
I was searching forum and see all different questions with dfferent ansvers but no one help for me.
I’m using CategoryDateAxis and whant to show to users vertical lines between days, weeks or month
Its depends from timestamp and visible period.
I need to opportunity to provide themself when to display vertical line.
In chart I display CandlestickSeries from stosk market and users whant to know when one day is ended and new day is begin.
How can I do that?
Hi Evgeny,
Thanks for your inquiry.
To be honest with you, I’m not sure that I understand what the problem is. Did you have a chance to take a look on our annotation tutorial? It shows an example how to add periodical annotation in realtime chart. You can even go further and recycle annotations instead of recreating them ( e.g. when annotation goes outside viewport instead of removing it you can update its position ).
The only thing which you need to consider that in case of using CategoryDateAxis you’ll need to assign index of Date in data series instead of Date itself. So when you’ll need to write a code which takes VisibleRange of xAxis, then gets it’s min/max values to get index range in viewport. Then you’ll need to iterate through your xValues within that range ( from min to max ) and insert annotations at required points.
Hope this will help you!
Best regards,
Yura
This is my code
@Override
public void onVisibleRangeChanged(IAxisCore iAxisCore, IRange iRange, IRange iRange1, boolean b) {
// 184.0 in my case
Double min = (Double) iAxisCore.getVisibleRange().getMin();
// 210.0 in my case
Double max = (Double) iAxisCore.getVisibleRange().getMax();
UpdateSuspender.using(mChart, new Runnable() {
@Override
public void run() {
final SciChartBuilder sciChartBuilder = SciChartBuilder.instance();
DateTime prevDate = null; // previous date for checking if new day
mChart.getAnnotations().clear();
for (Double i = min; i <= max; i++) { from 184.0 to 210.0
Date date = mOhlcDataSeries.getXValues().get(i.intValue()); // get first visible date
if (prevDate == null) { // save previous date and continue
prevDate = new DateTime(date.getTime()); // org.joda.time.DateTime
continue;
}
final DateTime dateTime = new DateTime(date.getTime());
// check if new day
if (!dateTime.withTimeAtStartOfDay().isEqual(prevDate.withTimeAtStartOfDay())) {
final VerticalLineAnnotation annotation = sciChartBuilder.newVerticalLineAnnotation()
.withXValue(i) // this not work
//.withX1(i) // this not work
.withVerticalGravity(Gravity.FILL_VERTICAL)
.withStroke(2, ColorUtil.Brown)
.withAnnotationLabel()
.withAnnotationLabel(LabelPlacement.TopRight, "Bottom-aligned", 90)
.build();
mChart.getAnnotations().add(annotation);
}
prevDate = dateTime;
}
}
});
dispatchVisibleRangeChanged();
}
I ckeck this code in CandlestickChartFragment from examples
I don’t know why but vertical lines desplayed
If I scroll chart then candles scrols BUT vertical lines stays in the same position
Labels in lines changes but positions not match candle positions
Hi Evgeny,
The line stays on same position because you’re using ‘i’ value as xValue for annotation and it’s calculated by incrementing min value of visible range in for loop. This means that if min = 0.5 then i values will be [0.5, 1.5, 2.5, …], if min = 0.7 then i will have [0.7, 1.7, 2.7, …] values an so on but the problem is that all these values will be translated to the same coordinates on screen ( min will be always transformed to 0 pixel on screen because it’s edge of the viewport and because other points are calculated by incrementing min value they will be transformed to same coordinates when you pan chart ).
Please try next code:
final VerticalLineAnnotation annotation = sciChartBuilder.newVerticalLineAnnotation()
.withXValue(i.intValue() - 0.5)
.build();
We need to substract 0.5 to shift annotation to the left with offset which equal to half of data point ( in category axis distance between two points is constant and equal to 1 because it works with indices ). This is required to prevent drawing of vertical line at the center of candle
Hope this will help you!
Best regards,
Yura
Please login first to submit.