我根据用户输入的公式在图表上绘制线条。如果用户输入多行,则无法绘制线条。
这是我的代码:
public void drawGraph(View v) {
EditText target = (EditText) findViewById(R.id.textView);
String equation = target.getText().toString();
mRenderer = new XYMultipleSeriesRenderer();
if (!equation.isEmpty()) {
String[] sides = equation.split("=");
List<String> variables = new ArrayList<String>();
String tempString = "";
if (sides.length > 1) {
String rightSide = sides[1];
char[] rightSideCharacters = rightSide.toCharArray();
for (char c : rightSideCharacters) {
if (Character.isLetter(c)) {
tempString = String.valueOf(c);
variables.add(String.valueOf(c));
}
}
boolean duplicates = duplicates(variables);
if (duplicates == false) {
lines++;
seriesRenderer = new XYSeriesRenderer();
seriesRenderer.setLineWidth(2);
seriesRenderer.setColor(Color.rgb(143, 2, 2));
seriesRenderer.setDisplayBoundingPoints(true);
seriesRenderer.setPointStyle(PointStyle.CIRCLE);
seriesRenderer.setPointStrokeWidth(3);
seriesRenderer2 = new XYSeriesRenderer();
seriesRenderer.setLineWidth(2);
seriesRenderer.setColor(Color.rgb(61, 90, 238));
seriesRenderer.setDisplayBoundingPoints(true);
seriesRenderer.setPointStyle(PointStyle.CIRCLE);
seriesRenderer.setPointStrokeWidth(3);
if (lines == 1) {
mRenderer.addSeriesRenderer(seriesRenderer);
}
if (lines == 2) {
mRenderer.addSeriesRenderer(seriesRenderer);
mRenderer.addSeriesRenderer(seriesRenderer2);
}
// We want to avoid black border
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00)); // transparent margins
mRenderer.setPanEnabled(false, false);
mRenderer.setYAxisMax(range);
mRenderer.setYAxisMin(-range);
mRenderer.setXAxisMax(range);
mRenderer.setXAxisMin(-range);
mRenderer.setShowGrid(true); // we show the grid
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
String originalRight = rightSide;
//System.out.println(range);
for (double i = -range; i < range; i++) {
rightSide = rightSide.replaceAll(tempString, "(" + String.valueOf(i) + ")");
math.setVariable(tempString, i);
if (lines == 1) {
graph.add(i, math.evaluate(rightSide.substring(1)));
}
if (lines == 2) {
System.out.println(i + "," + math.evaluate(rightSide.substring(1)));
graph2.add(i, math.evaluate(rightSide.substring(1)));
}
rightSide = originalRight;
}
if (lines == 1) {
dataset.addSeries(graph);
}
if (lines == 2) {
dataset.addSeries(graph);
dataset.addSeries(graph2);
}
chartView = ChartFactory.getLineChartView(this, dataset, mRenderer);
//chartView.repaint();
LinearLayout layout = (LinearLayout) findViewById(R.id.chartlayout);
layout.addView(chartView);
}
}
}
}
答案 0 :(得分:0)
seriesRenderer2 = new XYSeriesRenderer();
seriesRenderer.setLineWidth(2);
seriesRenderer.setColor(Color.rgb(61, 90, 238));
seriesRenderer.setDisplayBoundingPoints(true);
seriesRenderer.setPointStyle(PointStyle.CIRCLE);
seriesRenderer.setPointStrokeWidth(3);
应该有seriesRenderer2.setLineWidth(2);