@Override public void render(Canvas canvas) { try { chart.render(canvas); } catch (Exception e) { Log.e(TAG, e.toString()); } }
private void chartRender() { try { // 设置绘图区默认缩进px值,留置空间显示Axis,Axistitle.... int[] ltrb = getBarLnDefaultSpadding(); chart.setPadding(ltrb[0], ltrb[1], ltrb[2], ltrb[3]); // 限制Tickmarks可滑动偏移范围 chart.setXTickMarksOffsetMargin(ltrb[2] - 20.f); chart.setYTickMarksOffsetMargin(ltrb[3] - 20.f); // 显示边框 chart.showRoundBorder(); // 设定数据源 chart.setCategories(labels); chart.setDataSource(chartData); // 数据轴最大值 chart.getDataAxis().setAxisMax(100); // 数据轴刻度间隔 chart.getDataAxis().setAxisSteps(10); // 背景网格 chart.getPlotGrid().showHorizontalLines(); // chart.getPlotGrid().showVerticalLines(); chart.getPlotGrid().showEvenRowBgColor(); chart.getPlotGrid().showOddRowBgColor(); chart.getPlotGrid().getHorizontalLinePaint().setStrokeWidth(2); chart.getPlotGrid().setHorizontalLineStyle(XEnum.LineStyle.DASH); chart.getPlotGrid().setVerticalLineStyle(XEnum.LineStyle.DOT); chart.getPlotGrid().getHorizontalLinePaint().setColor(Color.RED); chart.getPlotGrid().getVerticalLinePaint().setColor(Color.BLUE); chart.setTitle("折线图(Line Chart)"); chart.addSubtitle("(XCL-Charts Demo)"); chart.getAxisTitle().setLowerTitle("(年份)"); // 激活点击监听 chart.ActiveListenItemClick(); // 为了让触发更灵敏,可以扩大5px的点击监听范围 chart.extPointClickRange(5); chart.showClikedFocus(); // 绘制十字交叉线 chart.showDyLine(); chart.getDyLine().setDyLineStyle(XEnum.DyLineStyle.Vertical); /* //想隐藏轴的可以下面的函数来隐藏 chart.getDataAxis().hide(); chart.getCategoryAxis().hide(); //想设置刻度线属性的可用下面函数 chart.getDataAxis().getTickMarksPaint() chart.getCategoryAxis().getTickMarksPaint() //想设置刻度线标签属性的可用下面函数 chart.getDataAxis().getAxisTickLabelPaint() chart.getCategoryAxis().getAxisTickLabelPaint() */ chart.getPlotArea().extWidth(100.f); // 调整轴显示位置 chart.setDataAxisLocation(XEnum.AxisLocation.RIGHT); chart.setCategoryAxisLocation(XEnum.AxisLocation.TOP); // 收缩绘图区右边分割的范围,让绘图区的线不显示出来 chart.getClipExt().setExtRight(0.f); // test x坐标从刻度线而不是轴开始 // chart.setXCoordFirstTickmarksBegin(true); // chart.getCategoryAxis().showTickMarks(); // chart.getCategoryAxis().setVerticalTickPosition(XEnum.VerticalAlign.MIDDLE); } catch (Exception e) { // TODO Auto-generated catch block Log.e(TAG, e.toString()); } }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 图所占范围大小 chart.setChartRange(w, h); }
// 触发监听 private void triggerClick(float x, float y) { // 交叉线 if (chart.getDyLineVisible()) chart.getDyLine().setCurrentXY(x, y); if (!chart.getListenItemClickStatus()) { // 交叉线 if (chart.getDyLineVisible()) this.invalidate(); } else { PointPosition record = chart.getPositionRecord(x, y); if (null == record) { if (chart.getDyLineVisible()) this.invalidate(); return; } LineData lData = chartData.get(record.getDataID()); Double lValue = lData.getLinePoint().get(record.getDataChildID()); float r = record.getRadius(); chart.showFocusPointF(record.getPosition(), r + r * 0.5f); chart.getFocusPaint().setStyle(Style.STROKE); chart.getFocusPaint().setStrokeWidth(3); if (record.getDataID() >= 3) { chart.getFocusPaint().setColor(Color.BLUE); } else { chart.getFocusPaint().setColor(Color.RED); } // 在点击处显示tooltip mPaintTooltips.setColor(Color.RED); // chart.getToolTip().setCurrentXY(x,y); chart.getToolTip().setCurrentXY(record.getPosition().x, record.getPosition().y); chart.getToolTip().addToolTip(" Key:" + lData.getLineKey(), mPaintTooltips); chart.getToolTip().addToolTip(" Label:" + lData.getLabel(), mPaintTooltips); chart.getToolTip().addToolTip(" Current Value:" + Double.toString(lValue), mPaintTooltips); // 当前标签对应的其它点的值 int cid = record.getDataChildID(); String xLabels = ""; for (LineData data : chartData) { if (cid < data.getLinePoint().size()) { xLabels = Double.toString(data.getLinePoint().get(cid)); chart.getToolTip().addToolTip("Line:" + data.getLabel() + "," + xLabels, mPaintTooltips); } } this.invalidate(); } }