[英]Androidplot chart is duplicating label values x-axis & y-axis
我正在使用androidplot显示图形,标签重复出现。 我搜索了论坛,并且stackoverflow尚未找到任何解决方案。
现在我注意到,如果x轴或y轴的arraysize小于10,则数组值将重复。
屏幕截图显示左轴重复值0,1,2。 x轴值越来越重复。 解决此重复标签问题的最佳建议是什么? 任何帮助将不胜感激。 如果您需要任何进一步的信息,请告诉我。
/**
* A simple XYPlot
*/
public class SimpleXYPlotActivity extends Activity {
private XYPlot plot;
String[] domainLabels;
String oldDateStr, newDateStr;
Date newDate;
int totalDays;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.graph_xy_plot_example);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.plot);
Intent intent = getIntent();
Bundle bd = intent.getExtras();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
if (bd != null) {
oldDateStr = (String) bd.get("START_DATE");
newDateStr = (String) bd.get("END_DATE");
plot.setTitle("From: "+oldDateStr+" To: "+newDateStr);
try {
Date date1 = format.parse(oldDateStr);
Date date2 = format.parse(newDateStr);
long diff = date2.getTime() - date1.getTime();
totalDays = Integer.parseInt(String.valueOf(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)));
System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
}
SimpleDateFormat formatterDateGraph = new SimpleDateFormat("MMM d");
SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
try {
newDate = format.parse(newDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
DatabaseHandler db = new DatabaseHandler(this);
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(newDate);
domainLabels= new String[totalDays];
Number[] series1Numbers = new Number[totalDays];
//domain 0 set outside loop for oldDateStr
domainLabels[0]=String.valueOf(formatterDateGraph.format(newDate.getTime()));
List<Orders> reportForDayList = db.getReportByTranType(oldDateStr, oldDateStr,"1");
series1Numbers[0]=reportForDayList.size();
for (int i = 1; i <totalDays ; i++) {
cal.add(Calendar.DAY_OF_YEAR, -1);
domainLabels[i] = String.valueOf(formatterDateGraph.format(cal.getTime()));
//fetch ordercount for that day
reportForDayList = db.getReportByTranType(formatterDate.format(cal.getTime()),formatterDate.format(cal.getTime()),"1");
series1Numbers[i]=reportForDayList.size();
}
series1Numbers = reverseSeries(series1Numbers);
domainLabels = reverseDomain(domainLabels);
//reverseNumber array
// domainLabels = reverseNumber(domainLabels);
// turn the above arrays into XYSeries':
// (Y_VALS_ONLY means use the element index as the x value)
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Sales");
/* XYSeries series2 = new SimpleXYSeries(
Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");*/
// create formatters to use for drawing a series using LineAndPointRenderer
// and configure them from xml:
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
LineAndPointFormatter series2Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add an "dash" effect to the series2 line:
/* series2Format.getLinePaint().setPathEffect(new DashPathEffect(new float[] {
// always use DP when specifying pixel sizes, to keep things consistent across devices:
PixelUtils.dpToPix(20),
PixelUtils.dpToPix(15)}, 0));*/
// just for fun, add some smoothing to the lines:
// see: http://androidplot.com/smooth-curves-and-androidplot/
series1Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
series2Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// plot.addSeries(series2, series2Format);
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new DecimalFormat("#"));
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
int i = Math.round(((Number) obj).floatValue());
return toAppendTo.append(domainLabels[i]);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
}
public static Number[] reverseSeries(Number[] a)
{
int l = a.length;
for (int j = 0; j < l / 2; j++)
{
Number temp = a[j];
a[j] = a[l - j - 1];
a[l - j - 1] = temp;
}
return a;
}
public static String[] reverseDomain(String[] a)
{
int l = a.length;
for (int j = 0; j < l / 2; j++)
{
String temp = a[j];
a[j] = a[l - j - 1];
a[l - j - 1] = temp;
}
return a;
}
}
您正在通过将6天的范围转换为长值,然后将这些值内的总范围细分为10个不同的细分来创建域值:您无法从6天的范围中获得10天的标签。
要解决此问题,您可以减少要显示的域段的数量,也可以显示更多天的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.