MPAndroidChart数据cad坐标小数点设置怎么设置

MPAndroidchart x轴能不能实现上下位置移动控制
MPAndroidchart x轴能不能实现左右位置移动控制 大神们,MPAndroidchart的X轴的位置能不能左右移动,默认的是最后一个坐标都是在最右边的Y轴上,但我需求时最后的坐标是显示在屏幕的中点,这功能怎么实现!大神们,这需求怎么破,我技术比较菜,求大神解答 --
MPAndroidchart x轴能不能实现左右位置移动控制大神们,MPAndroidchart&的X轴的位置能不能左右移动,默认的是最后一个坐标都是在最右边的Y轴上,但我需求时最后的坐标是显示在屏幕的中点,这功能怎么实现!大神们,这需求怎么破,我技术比较菜,求大神解答------解决思路----------------------新加数据的时候把你的整个数据集都刷新下,所有元素后移一位
你最喜欢的1790人阅读
Work随笔(8)
是实现图表功能的优秀控件, 可以完成大多数绘制需求. 对于修改第三方库而言, 优秀的架构是继承开发, 而不是把源码拆分出去. MP在显示标记控件(MarkView)时, 会有异常, 导致标志在图表边缘显示不全, 则需要重写控件解决问题.
继承LineChart, 提取高亮位置坐标getHighLightPos, 重绘标记drawMarkers.
* 数据中心的图表折线图, 继承MPChart的折线图
* Created by wangchenlong on 15/10/13.
public class CYDataLineChart extends LineChart {
@SuppressWarnings("unused")
private static final String TAG = "DEBUG-WCL: " + CYDataLineChart.class.getSimpleName();
public CYDataLineChart(Context context) {
super(context);
public CYDataLineChart(Context context, AttributeSet attrs) {
super(context, attrs);
public CYDataLineChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
public float[] getHighLightPos(Entry e, Highlight highlight) {
return getMarkerPosition(e, highlight);
protected void drawMarkers(Canvas canvas) {
if (mMarkerView == null || !mDrawMarkerViews || !valuesToHighlight())
Rect newRect = canvas.getClipBounds();
newRect.inset(-80, 0);
canvas.clipRect(newRect, Region.Op.REPLACE);
for (int i = 0; i & mIndicesToHighlight. i++) {
Highlight highlight = mIndicesToHighlight[i];
int xIndex = highlight.getXIndex();
if (xIndex &= mDeltaX && xIndex &= mDeltaX * mAnimator.getPhaseX()) {
Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]);
if (e == null || e.getXIndex() != mIndicesToHighlight[i].getXIndex())
float[] pos = getMarkerPosition(e, highlight);
float tmpY = pos[1] - 8 * AppUtils.getPerDp();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setColor(getResources().getColor(R.color.chart_circle));
canvas.drawCircle(pos[0], pos[1], 2 * AppUtils.getPerDp(), paint);
if (!mViewPortHandler.isInBounds(pos[0], tmpY))
mMarkerView.refreshContent(e, highlight);
mMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
mMarkerView.layout(0, 0, mMarkerView.getMeasuredWidth(),
mMarkerView.getMeasuredHeight());
if (tmpY - mMarkerView.getHeight() &= 0) {
float y = mMarkerView.getHeight() - tmpY;
mMarkerView.draw(canvas, pos[0], tmpY + y);
mMarkerView.draw(canvas, pos[0], tmpY);
getMarkerPosition是LineChart类中的protected方法, 继承类, 使用public方法导出.
float tmpY = pos[1] - 8 * AppUtils.getPerDp();, 重新计算Y坐标, 偏离原始画布.
但是这样做有一个问题, 在移动MarkView时, 父控件会有残留. 如何解决呢? 办法就是在移动时, 重绘父控件的canvas, 使用invalidate()函数.
mLcChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
int index = e.getXIndex();
Log.e(TAG, "index = " + index);
setChartIndex(index);
mCallback.setCurIndex(index);
float[] pos = mLcChart.getHighLightPos(e, h);
Log.e(TAG, "x: " + pos[0] + ", y: " + pos[1]);
mLlContainer.invalidate();
public void onNothingSelected() {
mLcChart.highlightValue(mIndex, 0);
private void setChartHighlight(int index) {
if (mLcChart.getData() == null) return;
mMarkerView.setDateText(mMarkers.get(index));
mLcChart.highlightValue(index, 0);
mLlContainer.invalidate();
在图表控件中, 内部外部都会触发高亮位置.
OK, Enjoy It!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1334470次
积分:22723
积分:22723
排名:第213名
原创:694篇
评论:324条
本人代码娴熟, 涉猎广泛.
欢迎Follow我的.
欢迎访问我的:
(5)(9)(10)(4)(11)(18)(11)(17)(11)(6)(15)(55)(1)(1)(13)(13)(18)(53)(24)(28)(61)(51)(21)(67)(56)(5)(3)(38)(51)(21)MPAndroidCharts 怎么设置y轴的网格线不显示_百度知道中国学网-中国IT综合信息门户
浏览器不支持嵌入式框架,或被配置为不显示嵌入式框架。
大类导航: |
列表导航:
友情链接:
因首页链接过多,部分BR小于5的已移入内页,有任何问题请联系QQ: (BR<5已移入内页)2688人阅读
Android MPAndroidChart(16)
其余文章索引:
时间仓促,难免有错误,有的话希望大家在评论中指出,谢谢。
一、ChartData 类
为了让大家更容易理解,这里先简单介绍下 MPAndroidChart 的数据模型 ChartData 。后面有文章再详细介绍该图标库的其它数据类型。
ChartData 类是所有数据类的基类,比如 LineData,BarData 等,它是用来为 Chart 提供数据的,通过 setData(ChartData data){...} 方法。
public class LineData extends ChartData { ...
以下提到的方法是在 ChartData 类中被实现,因此可用于所有子类。
1. Styling data
setDrawValues(boolean enabled) : 启用/禁用 绘制所有 DataSets 数据对象包含的数据的值文本。
setValueTextColor(int color) : 设置 DataSets 数据对象包含的数据的值文本的颜色。
setValueTextSize(float size) : 设置 DataSets 数据对象包含的数据的值文本的大小(单位是dp)。
LineDataSet dataSet = new LineDataSet(entryList, "dataLine");
dataSet.setLineWidth(2.5f);
dataSet.setCircleSize(4.5f);
dataSet.setHighLightColor(Color.RED);
dataSet.setDrawValues(true);
dataSet.setValueTextColor(Color.GREEN);
dataSet.setValueTextSize(12f);
allLinesList.add(dataSet);
setValueTypeface(Typeface tf) : 设置Typeface的所有价值标签的所有DataSets这些数据对象包含。
dataSet.setValueTextSize(24f);
allLinesList.add(dataSet);
Typeface tf1 = Typeface.createFromAsset(getAssets(), "OpenSans-BoldItalic.ttf");
dataSet.setValueTypeface(tf1);
Typeface tf2 = Typeface.createFromAsset(getAssets(), "OpenSans-LightItalic.ttf");
dataSet.setValueTypeface(tf2);
setValueFormatter(ValueFormatter f) : 为DataSets 数据对象包含的数据设置自定义的 ValueFormatter
2. Getters / Convenience
getDataSetByIndex(int index) : 返回目标 DataSet 列表中给定索引的数据对象。
contains(Entry entry) : 检查此数据对象是否包含指定的Entry 。 注:这个相当影响性能,性能严峻情况下,不要过度使用。
contains(T dataSet) : Returns true if this data object contains the provided DataSet , false if not.
clearValues() : 清除所有 DataSet 对象和所有 Entries 的数据 。 不会删除所提供的 x-values 。
4, 选中高亮
setHighlightEnabled(boolean enabled) : 设置为true,允许通过点击高亮突出 ChartData 对象和其 DataSets 。
二、设置数据
如果你想为图表添加数据,你可以通过下面这个方法:
public void setData(ChartData data) { ... }
基类 ChartData 封装了渲染过程中所需要的图表中的所有数据和信息。 对于每种类型的图表,不同的 ChartData 子类(例如 LineData)应该被用于为图表设置数据。 在构造函数中,你可以通过 ArrayList&? extends DataSet& 作为要显示的值,一个额外 ArrayList 的 String 用来描述 x 轴上的标签。 例如,通过 LineData 将数据添加到一个 LineChart :
public LineData(ArrayList&String& xVals, ArrayList&LineDataSet& sets) { ... }
那么,DataSet 到底是什么和为什么你会需要它? 很简单,一个 DataSet 对象代表一组 entries(数据类型 Entry),在图表内属于一个整体。 它在图表中被设计成 逻辑上分离的不同组的值 。 每种类型的图表,通过一个不同的 DataSet 对象(如 LineDataSet)来做出特定的 style 。
2. 举个例子
你可能想用 LineChart 来 显示两个不同公司一年的季度收入 。在这种情况下,将建议创建两个不同的 LineDataSet 对象,每个包含四个值(四个季度)。 用一个 ArrayList 描述在x轴上的标签,您只需提供四个 String : “第一季度”,“第二季度”,“第三季度”,“第四季度” 。
当然,也有可能只提供一个包含两个公司的所有8个值的 LineDataSet 对象。
那么,如何建立一个 LineDataSet 对象?
public class LineDataSet extends LineRadarDataSet&Entry& {
public LineDataSet(List&Entry& yVals, String label) {
super(yVals, label);
mCircleColors = new ArrayList&Integer&();
mCircleColors.add(Color.rgb(140, 234, 255));
在构造函数中,很明显 LineDataSet 需要一个 Entry
的 ArrayList 参数,和一个 String 参数作为 图例 (Legend)的 label 来描述 LineDataSet
。 此外, this label can be used to find the LineDataSet amongst other LineDataSet objects in the LineData object.
Entry 类型的 ArrayList 封装了图表所有的值。 Entry 相当一个容器,用来封装并保存“一对值”,and it’s position on the x-axis ( the index inside the ArrayList of String of the LineData object the value is mapped to ) :
public Entry(float val, int xIndex) { ... }
3. 还是用之前的例子
以前面提到的(这两家公司一年的季度营收)为例:
0) 总的数据包含和操作类似:
new Entry(float val, int xIndex);
new ArrayList&Entry&();
new LineDataSet(ArrayList&Entry& , "label");
new ArrayList&LineDataSet&();
new LineData(List&String& xVals, List&LineDataSet& dataSets));
chart.setData(LineData);
1) 首先,创建类型的列表Entry ,将保留您的值:
ArrayList&Entry& valsComp1 = new ArrayList&Entry&();
ArrayList&Entry& valsComp2 = new ArrayList&Entry&();
2) 然后,给 lists 集合添加 Entry 对象。
确保 Entry 对象包含的 index 都是正确的 (对于x轴来说)。
Entry c1e1 = new Entry(100.000f, 0);
valsComp1.add(c1e1);
Entry c1e2 = new Entry(50.000f, 1);
valsComp1.add(c1e2);
Entry c2e1 = new Entry(120.000f, 0);
valsComp2.add(c2e1);
Entry c2e2 = new Entry(110.000f, 1);
valsComp2.add(c2e2);
3) 现在,有了 Entry 对象的 lists 集合,再创建 LineDataSet 对象:
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
setComp1.setAxisDependency(AxisDependency.LEFT);
LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
setComp2.setAxisDependency(AxisDependency.LEFT);
通过调用 setAxisDependency(...) 使得
DataSet 对应指定轴,进行绘制。
4) 通过 DataSets 集合和
x-axis entries 集合,来创建我们的 ChartData 对象:
ArrayList&LineDataSet& dataSets = new ArrayList&LineDataSet&();
dataSets.add(setComp1);
dataSets.add(setComp2);
ArrayList&String& xVals = new ArrayList&String&();
xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");
LineData data = new LineData(xVals, dataSets);
mLineChart.setData(data);
mLineChart.invalidate();
调用 invalidate() 后图表被刷新,所提供的数据重新绘制。
三、设置颜色
1. ColorTemplate 类简介
Class that holds predefined color integer arrays (e.g. ColorTemplate.VORDIPLOM_COLORS) and convenience methods for loading colors from resources.
该类封装有 预定义的颜色整数数组(例如 ColorTemplate.VORDIPLOM_COLORS)和便利的从资源加载颜色的方法。
ColorTemplate 类的源码
public class ColorTemplate {
* an "invalid" color that indicates that no color is set
public static final int COLOR_NONE = -1;
* this "color" is used for the Legend creation and indicates that the next
* form should be skipped
public static final int COLOR_SKIP = -2;
* THE COLOR THEMES ARE PREDEFINED (predefined color integer arrays), FEEL
* FREE TO CREATE YOUR OWN WITH AS MANY DIFFERENT COLORS AS YOU WANT
public static final int[] LIBERTY_COLORS = {
Color.rgb(207, 248, 246), Color.rgb(148, 212, 212), Color.rgb(136, 180, 187),
Color.rgb(118, 174, 175), Color.rgb(42, 109, 130)
public static final int[] JOYFUL_COLORS = {
Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)
public static final int[] PASTEL_COLORS = {
Color.rgb(64, 89, 128), Color.rgb(149, 165, 124), Color.rgb(217, 184, 162),
Color.rgb(191, 134, 134), Color.rgb(179, 48, 80)
public static final int[] COLORFUL_COLORS = {
Color.rgb(193, 37, 82), Color.rgb(255, 102, 0), Color.rgb(245, 199, 0),
Color.rgb(106, 150, 31), Color.rgb(179, 100, 53)
public static final int[] VORDIPLOM_COLORS = {
Color.rgb(192, 255, 140), Color.rgb(255, 247, 140), Color.rgb(255, 208, 140),
Color.rgb(140, 234, 255), Color.rgb(255, 140, 157)
* Returns the Android ICS holo blue light color.
public static int getHoloBlue() {
return Color.rgb(51, 181, 229);
* turn an array of resource-colors (contains resource-id integers) into an
* array list of actual color integers
* colors an integer array of resource id's of colors
public static List&Integer& createColors(Resources r, int[] colors) {
List&Integer& result = new ArrayList&Integer&();
for (int i : colors) {
result.add(r.getColor(i));
* Turns an array of colors (integer color values) into an ArrayList of
public static List&Integer& createColors(int[] colors) {
List&Integer& result = new ArrayList&Integer&();
for (int i : colors) {
result.add(i);
当时自从 MPAndroidChart V1.4.0 之后,ColorTemplate 这个类就不再重要了。我们可以直接通过 DataSet 对象进行指定颜色,从而可以区分每个 DataSet 的 Style 。
2. 举例说明
还是用公司季度营收的例子,我们有两个不同的 LineDataSet 代表两家公司,为此我们现在要设置不同的颜色。
我们希望:
“公司1” 的颜色用四种不同变化“红”来表示
“连队2” 的颜色用四种不同变化“绿”来表示
(效果如下面效果图的右图,左图是单色的)
效果图如下所示:
ArrayList&LineDataSet& allLinesList = new ArrayList&LineDataSet&();
LineDataSet dataSet1 = new LineDataSet(entryList1, "dataLine1");
LineDataSet dataSet2 = new LineDataSet(entryList2, "dataLine2");
dataSet1.setColors(new int[]{R.color.red1, R.color.red2, R.color.red3,
R.color.red4}, context);
dataSet2.setColors(new int[]{R.color.green1, R.color.green2, R.color.green3,
R.color.green4}, context);
allLinesList.add(dataSet1);
allLinesList.add(dataSet2);
List&String& quarterStrs = new ArrayList&String&();
quarterStrs.add("第一季度");
quarterStrs.add("第二季度");
quarterStrs.add("第三季度");
quarterStrs.add("第四季度");
LineData mChartData = new LineData(quarterStrs, allLinesList);
除此之外,还有很多其他的方法来设置 DataSet 的颜色 。 下面是完整的文档:
setColors(int [] colors, Context c) : 设置该 DataSet 的颜色。
您可以使用 new int[] {R.color.red,R.color.green,...} 使得颜色值可以重用。 在内部,颜色是使用 getResources().getColor(...) 来实现获取的。
setColors(int [] colors) : 设置该 DataSet 的颜色。Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
setColors(ArrayList&Integer& colors) : 设置该 DataSet 的颜色。Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
setColor(int color) : 设置该数据集 唯一的颜色。 在内部,实现方式类似上面的”颜色数组”,只不过”颜色数组都是同一种颜色”
3. ColorTemplate 的使用例子:
LineDataSet set = new LineDataSet(...);
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
如果没有为 DataSet 设置颜色,则使用默认颜色。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:54257次
积分:1394
积分:1394
排名:千里之外
原创:62篇
评论:30条
(1)(1)(18)(13)(8)(2)(14)(2)(1)(1)(1)

我要回帖

更多关于 excel坐标轴小数点 的文章

 

随机推荐