@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 图所占范围大小 chart.setChartRange(w, h); chart2.setChartRange(w, h); }
private void drawDyLegend() { Legend dyLegend = chart.getDyLegend(); if (null == dyLegend) return; dyLegend.setPosition(0.8f, 0.5f); if (chart.getPlotArea().getHeight() > chart.getPlotArea().getWidth()) { dyLegend.setPosition(0.6f, 0.5f); } // dyLegend.setColSpan(30.f); dyLegend.getBackgroundPaint().setColor(Color.BLACK); dyLegend.getBackgroundPaint().setAlpha(100); dyLegend.setRowSpan(20.f); dyLegend.setMargin(15.f); dyLegend.setStyle(XEnum.DyInfoStyle.ROUNDRECT); Paint pDyLegend = new Paint(Paint.ANTI_ALIAS_FLAG); pDyLegend.setColor(Color.GREEN); PlotDot dotDyLegend = new PlotDot(); dotDyLegend.setDotStyle(XEnum.DotStyle.RECT); dyLegend.addLegend(dotDyLegend, "库可用xxx(PB)", pDyLegend); Paint pDyLegend2 = new Paint(Paint.ANTI_ALIAS_FLAG); pDyLegend2.setColor(Color.RED); dyLegend.addLegend(dotDyLegend, "库已用xxx(PB)", pDyLegend2); Paint pDyLegend3 = new Paint(Paint.ANTI_ALIAS_FLAG); pDyLegend3.setColor(Color.CYAN); dyLegend.addLegend(dotDyLegend, "未分配xxx(PB)", pDyLegend3); Paint pDyLegend4 = new Paint(Paint.ANTI_ALIAS_FLAG); pDyLegend4.setColor(Color.YELLOW); dyLegend.addLegend("总计:xxx(PB)", pDyLegend4); }
@Override public void render(Canvas canvas) { try { chart.render(canvas); chartPie.render(canvas); } catch (Exception e) { Log.e(TAG, e.toString()); } }
private void chartAnimation() { try { chart.getDataAxis().hide(); chart.getPlotLegend().hide(); int[] ltrb = getBarLnDefaultSpadding(); for (int i = 8; i > 0; i--) { Thread.sleep(100); chart.setPadding(ltrb[0], i * ltrb[1], ltrb[2], ltrb[3]); if (1 == i) { drawLast(); drawDyLegend(); } postInvalidate(); } } catch (Exception e) { Thread.currentThread().interrupt(); } }
@Override public void render(Canvas canvas) { try { chart.render(canvas); chart2.render(canvas); // 绘制轴点 float radius = 10f; Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(this.axisColor); canvas.drawCircle( chart2.getPlotArea().getLeft(), chart2.getPlotArea().getBottom(), radius, paint); canvas.drawCircle( chart2.getPlotArea().getRight(), chart2.getPlotArea().getBottom(), radius, paint); canvas.drawCircle( chart2.getPlotArea().getLeft(), chart2.getPlotArea().getTop(), radius, paint); } catch (Exception e) { Log.e(TAG, e.toString()); } }
// 触发监听 private void triggerClick(float x, float y) { // 交叉线 if (chart.getDyLineVisible()) chart.getDyLine().setCurrentXY(x, y); if (!chart.getListenItemClickStatus()) { // 交叉线 if (chart.getDyLineVisible() && chart.getDyLine().isInvalidate()) this.invalidate(); } else { BarPosition record = chart.getPositionRecord(x, y); if (null == record) { if (chart.getDyLineVisible()) this.invalidate(); return; } if (record.getDataID() >= chartData.size()) return; BarData bData = chartData.get(record.getDataID()); if (record.getDataChildID() >= bData.getDataSet().size()) return; Double bValue = bData.getDataSet().get(record.getDataChildID()); // 显示选中框 chart.showFocusRectF(record.getRectF()); chart.getFocusPaint().setStyle(Style.STROKE); chart.getFocusPaint().setStrokeWidth(3); chart.getFocusPaint().setColor(Color.GREEN); // 在点击处显示tooltip mPaintToolTip.setAntiAlias(true); mPaintToolTip.setColor(bData.getColor()); mDotToolTip.setDotStyle(XEnum.DotStyle.RECT); mDotToolTip.setColor(Color.BLUE); // bData.getColor()); // 位置显示方法一: // 用下列方法可以让tooltip显示在柱形顶部 // chart.getToolTip().setCurrentXY(record.getRectF().centerX(),record.getRectF().top); // 位置显示方法二: // 用下列方法可以让tooltip在所点击位置显示 chart.getToolTip().setCurrentXY(x, y); chart.getToolTip().setStyle(XEnum.DyInfoStyle.ROUNDRECT); chart.getToolTip().addToolTip(mDotToolTip, bData.getKey(), mPaintToolTip); chart.getToolTip().addToolTip("数量:" + Double.toString(bValue), mPaintToolTip); chart.getToolTip().getBackgroundPaint().setAlpha(100); chart.getToolTip().setAlign(Align.CENTER); chart.getToolTip().setInfoStyle(XEnum.DyInfoStyle.CIRCLE); // chart.getToolTip().getBackgroundPaint().setColor(Color.rgb(30, 30, 30)); this.invalidate(); } }
private void drawLast() { // 标题 chart.setTitle("数据库统计"); chart.addSubtitle("(XCL-Charts Demo)"); chart.getPlotTitle().getTitlePaint().setColor(colorTitalAxes); chart.getPlotTitle().getSubtitlePaint().setColor(colorTitalAxes); // 激活点击监听 chart.ActiveListenItemClick(); chart.showClikedFocus(); // 扩展横向显示范围,当数据太多时可用这个扩展实际绘图面积 // chart.getPlotArea().extWidth(200f); // 禁用平移模式 chart.disablePanMode(); // 限制只能左右滑动 // chart.setPlotPanMode(XEnum.PanMode.HORIZONTAL); // 禁用双指缩放 // chart.disableScale(); chart.getDataAxis().show(); chart.getPlotLegend().show(); // 当值与轴最小值相等时,不显示轴 chart.hideBarEqualAxisMin(); // 批注 List<AnchorDataPoint> mAnchorSet = new ArrayList<AnchorDataPoint>(); AnchorDataPoint an1 = new AnchorDataPoint(2, 0, XEnum.AnchorStyle.CIRCLE); an1.setAlpha(200); an1.setBgColor(Color.rgb(203, 189, 231)); // an1.setAreaStyle(XEnum.DataAreaStyle.FILL); AnchorDataPoint an2 = new AnchorDataPoint(1, 1, XEnum.AnchorStyle.CIRCLE); // CIRCLE an2.setBgColor(Color.GRAY); AnchorDataPoint an3 = new AnchorDataPoint(0, 2, XEnum.AnchorStyle.RECT); // CAPROUNDRECT RECT an3.setBgColor(Color.rgb(255, 145, 126)); AnchorDataPoint an4 = new AnchorDataPoint(0, 1, XEnum.AnchorStyle.CAPRECT); an4.setBgColor(Color.rgb(255, 145, 126)); an4.setAnchor("我是批注"); mAnchorSet.add(an1); mAnchorSet.add(an2); mAnchorSet.add(an3); mAnchorSet.add(an4); chart.setAnchorDataPoint(mAnchorSet); chart.setApplyBackgroundColor(true); chart.setBackgroundColor( XEnum.Direction.VERTICAL, Color.rgb(69, 117, 180), Color.rgb( 224, 243, 248)); // Color.rgb(17, 162, 255),Color.rgb(163, 219, 254));//Color.WHITE); chart.getBorder().setBorderLineColor(Color.rgb(181, 64, 1)); chart.getBorder().getLinePaint().setStrokeWidth(3); chart.getPlotArea().setBackgroundColor(true, colorPlotArea); // chart.getPlotArea().setApplayGradient(true); // chart.getPlotArea().setGradientDirection(XEnum.Direction.VERTICAL); // chart.getPlotArea().setBeginColor(Color.rgb(116, 174, 210)); // chart.getPlotArea().setEndColor(Color.WHITE); chart.showRoundBorder(); CustomLineData line1 = new CustomLineData("分界", 60d, Color.rgb(218, 198, 61), 7); line1.setCustomLineCap(XEnum.DotStyle.HIDE); line1.setLabelHorizontalPostion(Align.RIGHT); // line1.setLabelOffset(15); line1.getLineLabelPaint().setColor(Color.RED); mCustomLineDataset.add(line1); chart.setCustomLines(mCustomLineDataset); // 饼图 float pieWH = DensityUtil.dip2px(getContext(), 70); float pieX = chart.getPlotArea().getRight() - pieWH * 3; chartPie.setChartRange(pieX, pieWH, pieWH, pieWH); }
private void chartRender() { try { // 设置绘图区默认缩进px值,留置空间显示Axis,Axistitle.... int[] ltrb = getBarLnDefaultSpadding(); chart.setPadding(ltrb[0], ltrb[1], ltrb[2], ltrb[3]); // 数据源 chart.setDataSource(chartData); chart.setCategories(chartLabels); // 轴标题 chart.getAxisTitle().setLeftTitle("数据库"); chart.getAxisTitle().setLowerTitle("分布位置"); // 数据轴 chart.getDataAxis().setAxisMax(100); chart.getDataAxis().setAxisMin(0); chart.getDataAxis().setAxisSteps(5); // 定义数据轴标签显示格式 chart .getDataAxis() .setLabelFormatter( new IFormatterTextCallBack() { @Override public String textFormatter(String value) { // TODO Auto-generated method stub Double tmp = Double.parseDouble(value); DecimalFormat df = new DecimalFormat("#0"); String label = df.format(tmp).toString(); return (label); } }); // 在柱形顶部显示值 chart.getBar().setItemLabelVisible(true); // 设定格式 chart.setItemLabelFormatter( new IFormatterDoubleCallBack() { @Override public String doubleFormatter(Double value) { // TODO Auto-generated method stub DecimalFormat df = new DecimalFormat("#0"); String label = df.format(value).toString(); return label; } }); // 让柱子间没空白 // chart.getBar().setBarInnerMargin(0d); // 轴颜色 chart.getDataAxis().getAxisPaint().setColor(colorTitalAxes); chart.getCategoryAxis().getAxisPaint().setColor(colorTitalAxes); chart.getDataAxis().getTickMarksPaint().setColor(colorTitalAxes); chart.getCategoryAxis().getTickMarksPaint().setColor(colorTitalAxes); chart.getDataAxis().getTickLabelPaint().setColor(colorTitalAxes); chart.getCategoryAxis().getTickLabelPaint().setColor(colorTitalAxes); chart.getAxisTitle().getLeftTitlePaint().setColor(colorTitalAxes); chart.getAxisTitle().getLowerTitlePaint().setColor(colorTitalAxes); chart.getBar().getItemLabelPaint().setColor(Color.rgb(246, 133, 39)); chart.getBar().getItemLabelPaint().setTextSize(15); // 指隔多少个轴刻度(即细刻度)后为主刻度 chart.getDataAxis().setDetailModeSteps(5); // 显示十字交叉线 chart.showDyLine(); DyLine dyl = chart.getDyLine(); if (null != dyl) { dyl.setDyLineStyle(XEnum.DyLineStyle.Horizontal); dyl.setLineDrawStyle(XEnum.LineStyle.DASH); } // 数据轴居中显示 // chart.setDataAxisLocation(XEnum.AxisLocation.VERTICAL_CENTER); // 忽略Java的float计算误差,提高性能 // chart.disableHighPrecision(); // 柱形和标签居中方式 // chart.setBarCenterStyle(XEnum.BarCenterStyle.TICKMARKS); } catch (Exception e) { // TODO Auto-generated catch block Log.e(TAG, "chartRender():" + e.toString()); } }
private void chartRender() { try { // 设置绘图区默认缩进px值,留置空间显示Axis,Axistitle.... int[] ltrb = getBarLnDefaultSpadding(); chart.setPadding(mOffset, ltrb[1], ltrb[2], ltrb[3]); chart.getBar().setBarStyle(XEnum.BarStyle.FILL); chart.setApplyBackgroundColor(true); chart.setBackgroundColor(Color.rgb(19, 163, 224)); chart.showRoundBorder(); // 数据源 chart.setDataSource(chartData); chart.setCategories(chartLabels); // 数据轴 chart.getDataAxis().setAxisMax(2500); chart.getDataAxis().setAxisMin(0); chart.getDataAxis().setAxisSteps(500); // 让柱子间没空白 chart.getBar().setBarInnerMargin(0.f); // 隐藏轴 chart.getDataAxis().hide(); chart.getCategoryAxis().hide(); } catch (Exception e) { // TODO Auto-generated catch block Log.e(TAG, e.toString()); } }
private void chartRender2() { try { // 设置绘图区默认缩进px值,留置空间显示Axis,Axistitle.... int[] ltrb = getBarLnDefaultSpadding(); chart2.setPadding(mOffset, ltrb[1], ltrb[2], ltrb[3]); // 显示边框 // chart2.showRoundBorder(); // chart.setApplyBackgroundColor(true); chart2.setBackgroundColor(Color.rgb(19, 163, 224)); chart2.showRoundBorder(); chart2.getBar().setBarStyle(XEnum.BarStyle.FILL); // 标题 chart2.setTitle("负债率标准: 40%~60%"); chart2.addSubtitle("(XCL-Charts Demo)"); chart2.getPlotTitle().getTitlePaint().setColor(axisColor); chart2.getPlotTitle().getSubtitlePaint().setColor(axisColor); // 数据源 chart2.setDataSource(chartData2); chart2.setCategories(chartLabels); // 轴标题 chart2.getAxisTitle().setLeftTitle("金额"); chart2.getAxisTitle().setLowerTitle("资产负债率"); // 数据轴 chart2.getDataAxis().setAxisMax(2500); chart2.getDataAxis().setAxisMin(0); chart2.getDataAxis().setAxisSteps(500); // 定义数据轴标签显示格式 chart2 .getDataAxis() .setLabelFormatter( new IFormatterTextCallBack() { @Override public String textFormatter(String value) { // TODO Auto-generated method stub Double tmp = Double.parseDouble(value); DecimalFormat df = new DecimalFormat("#0"); String label = df.format(tmp).toString(); return (label); } }); // 在柱形顶部显示值 chart2.getBar().setItemLabelVisible(true); // 设定格式 chart2.setItemLabelFormatter( new IFormatterDoubleCallBack() { @Override public String doubleFormatter(Double value) { // TODO Auto-generated method stub DecimalFormat df = new DecimalFormat("#0"); String label = df.format(value).toString(); return label; } }); // 让柱子间没空白 chart2.getBar().setBarInnerMargin(0.f); // 轴颜色 chart2.getDataAxis().getAxisPaint().setColor(axisColor); chart2.getCategoryAxis().getAxisPaint().setColor(axisColor); chart2.getDataAxis().getTickMarksPaint().setColor(axisColor); chart2.getCategoryAxis().getTickMarksPaint().setColor(axisColor); chart2.getDataAxis().getTickLabelPaint().setColor(axisColor); chart2.getCategoryAxis().getTickLabelPaint().setColor(axisColor); chart2.getAxisTitle().getLeftTitlePaint().setColor(axisColor); chart2.getAxisTitle().getLowerTitlePaint().setColor(axisColor); // 隐藏图例 chart2.getPlotLegend().hide(); // chart2.showRoundBorder(); } catch (Exception e) { // TODO Auto-generated catch block Log.e(TAG, e.toString()); } }