我正在使用TeeChart库在Android上显示烛台图。
现在我正在开发一个简单的SWT应用程序来测试库的一些功能。
我用
给我的图表添加了大约400个蜡烛 series.add(c.getOpen(), c.getHigh(), c.getLow(), c.getClose())
当有很多蜡烛展示时,它们开始相互重叠。
避免这种情况的最好方法是什么?我的想法是计算蜡烛的数量,可以使用图表宽度,蜡烛宽度和蜡烛之间的一些间距来显示而不重叠:
private int getNumberOfCandlesVisible() {
final Candle candles = (Candle) series;
final int panelWidth = chart.getWidth();
final int candleWidth = candles.getCandleWidth();
return panelWidth / (candleWidth + CANDLE_SPACING);
}
然后我尝试显示最后N个值,如果series.getCount > getNumberOfCandlesVisible()
使用缩放或setMinMax()
一个轴。这条路对吗?我想滚动图表以显示大多数实际值,但它似乎并不顺利…也许有别的办法?
和这个很像,不是吗?
那么,我想这里的主要问题是如何平稳过渡。要做到这一点,你可以在Android中使用TimerTask。即:
public class MainActivity extends Activity {
Timer timer;
MyTimerTask myTimerTask;
int cycleCount;
class MyTimerTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
cycleCount++;
if (cycleCount <= 60) {
double tmpIncr = (tChart1.getSeries(0).getXValues().getLast() - tChart1.getSeries(0).getXValues().getValue(tChart1.getSeries(0).getCount()-2)) / 60;
tChart1.getAxes().getBottom().setAutomaticMaximum(false);
tChart1.getAxes().getBottom().setMaximum(tChart1.getAxes().getBottom().getMaximum()+tmpIncr);
tChart1.refreshControl();
} else {
startStopTimer(false);
}
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout group = (LinearLayout) findViewById(R.id.chart_layout1);
createChart(group);
initializeChart();
}
private TChart tChart1;
private int timerInterval, testIndex;
private int chartPaintCtr;
private int pointsRefreshed, pointsPlotted;
private float myDensity;
private void createChart(LinearLayout group) {
tChart1 = new TChart(this);
group.addView(tChart1);
}
private void initializeChart() {
// apply theme
ThemesList.applyTheme(tChart1.getChart(), 1);
// multitouch drag zoom
tChart1.getZoom().setZoomStyle(ZoomStyle.INCHART_MULTI);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
myDensity = metrics.density;
tChart1.getAspect().setFontZoom(
tChart1.getAspect().getFontZoom() * myDensity);
// hide things for better speed
tChart1.getAspect().setView3D(false);
tChart1.getLegend().setVisible(false);
tChart1.getFooter().setVisible(false);
tChart1.getHeader().setVisible(false);
tChart1.getWalls().setVisible(false);
tChart1.getZoom().setAnimated(true);
tChart1.getZoom().setAnimatedSteps(15);
FastLine lineSeries1 = new FastLine(tChart1.getChart());
lineSeries1.fillSampleValues();
tChart1.getAxes().getTop().getGrid().setVisible(false);
tChart1.getAxes().getRight().getGrid().setVisible(false);
int sep = 150;
tChart1.getAxes().getTop().getLabels().setSeparation(sep);
tChart1.getAxes().getRight().getLabels().setSeparation(sep);
tChart1.getAxes().getBottom().getLabels().setSeparation(sep);
tChart1.getAxes().getLeft().getLabels().setSeparation(sep);
tChart1.getPanel().setMarginLeft(7);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tChart1.getZoom().setAnimated(false);
msgSet = false;
chartPaintCtr = 0;
cycleCount = 0;
Random generator = new Random();
int tmpRandom = generator.nextInt((int)tChart1.getSeries(0).getYValues().getRange()/10);
tChart1.setAutoRepaint(false);
tChart1.getSeries(0).add(50, tChart1.getSeries(0).getYValues().getLast()-(int)tChart1.getSeries(0).getYValues().getRange()/20+tmpRandom);
tChart1.setAutoRepaint(true);
startStopTimer(true);
}
});
}
public void startStopTimer(Boolean run) {
if (run) {
if (timer != null) {
timer.cancel();
}
// re-schedule timer here
// otherwise, IllegalStateException of
// "TimerTask is scheduled already"
// will be thrown
timer = new Timer();
myTimerTask = new MyTimerTask();
// delay to first xxms, repeat in xxms
timer.schedule(myTimerTask, 0, timerInterval);
} else {
if (timer != null) {
timer.cancel();
timer = null;
}
}
}
}
在上面的例子中,我们有一个包含25个值的FastLine系列。然后,当我们按下按钮时,我们在远处添加另一个点(50)。