我终于在我的应用程序中添加了三角图。该图很好地显示,但在缩放或平移时它不会更新,所以我添加了更新代码,但是当它更新时,它会显示奇怪的线条,如照片中所示:
我的代码:
public void addFunction() {
layout.removeAllViewsInLayout();
layout.addView(mChartView, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
undo.setVisibility(View.VISIBLE);
mChartView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
mRenderer.setZoomEnabled(true);
double[] Xs;
double[] values;
int step = 4;
int count = 360 / step + 1;
Xs = (new double[count]);
values = new double[count];
for (int i = 0; i < count; i++) {
int angle = i * step;
Xs[i] = angle;
double rAngle = Math.toRadians(angle);
values[i] = Math.sin(rAngle);
}
XYSeriesRenderer renderer = new XYSeriesRenderer();
// set some renderer properties
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
XYSeries series = new XYSeries("Trig", 0);
int seriesLength = Xs.length;
for (int k = 0; k < seriesLength; k++) {
series.add(Xs[k], values[k]);
}
mRenderer.addSeriesRenderer(renderer);
mDataset.addSeries(series);
mCurrentSeries = series;
mChartView.repaint();
mChartView.addPanListener(new PanListener() {
@Override
public void panApplied() {
updateGraph();
}
});
mChartView.addZoomListener(new ZoomListener() {
@Override
public void zoomReset() {
updateGraph();
}
@Override
public void zoomApplied(ZoomEvent e) {
updateGraph();
}
}, true, true);
}
这是更新图形的代码:
public void updateGraph() {
final double minY = mRenderer.getYAxisMin();
final double maxY = mRenderer.getYAxisMax();
final double minX = mRenderer.getXAxisMin();
final double maxX = mRenderer.getXAxisMax();
double lastX = (maxX-minX)/2+minX;
double lastY = (maxY-minY)/2+minY;
for(double x=minX;x<=maxX;x+=(0.00125*(maxX-minX))) {
double rAngle = Math.toRadians(x);
double y = Math.sin(rAngle);
if(pointIsNaN(lastY, y, maxY, minY)) {
mCurrentSeries.add(x, MathHelper.NULL_VALUE);
}
else if(pointIsNaN(lastX, x, maxX, minX)) {
mCurrentSeries.add(MathHelper.NULL_VALUE, y);
}
else{
mCurrentSeries.add(x, y);
}
}
mChartView.repaint();
}