我是功能上要求数据是x的数值由大往小开始绘制折线图。
举例:我的x的数值count是5,4,3,2,1,0;y的数值count*40+500;
- 我的xy轴的大小都是固定的。
- 其中的添加到entry集合,然后存入LineDataSet,再LineData这种顺序就自己找demo。
- 内核是我对entry集合进行排序,用到的函数是
Collections.sort(values3,new EntryXComparator());
其中values3是我定义的ArrayList<Entry> values3 = new ArrayList<>();
这应该可以理解吧 - 排序完之后,要告知数值更新,我是LineDataSet定义的set3进行更新,用的是
set3.notifyDataSetChanged();
再接着用LineChart的invalidate()
以上我的想法和实施
下面是我的具体代码,包括xml和其中的Java,我是直接copympandroidchart的一个demo搞的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add3"
android:text="添加3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/add3"
android:id="@+id/add2"
android:text="添加2" />
</RelativeLayout>
package com.linkzill.linecharttest;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.EntryXComparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends DemoBase implements SeekBar.OnSeekBarChangeListener,
OnChartValueSelectedListener {
private LineChart chart;
private Button addbtn,addbtn1;
private SeekBar seekBarX, seekBarY;
private TextView tvX, tvY;
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setTitle("LineChartActivity2");
chart = findViewById(R.id.chart1);
addbtn=findViewById(R.id.add3);
addbtn1=findViewById(R.id.add2);
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setData(3,30);
}
});
addbtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setData(2,30);
}
});
chart.setOnChartValueSelectedListener(this);
chart.getDescription().setEnabled(false);
chart.setTouchEnabled(true);
chart.setDragDecelerationFrictionCoef(0.9f);
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
chart.setHighlightPerDragEnabled(true);
chart.setPinchZoom(true);
chart.setBackgroundColor(Color.LTGRAY);
chart.animateX(2000);
Legend l = chart.getLegend();
l.setForm(Legend.LegendForm.LINE);
l.setTypeface(tfLight);
l.setTextSize(11f);
l.setTextColor(Color.WHITE);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
XAxis xAxis = chart.getXAxis();
xAxis.setTypeface(tfLight);
xAxis.setTextSize(11f);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(7);
xAxis.setAxisMinimum(0);
xAxis.setDrawAxisLine(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
chart.getAxisLeft().setEnabled(false);
YAxis rightAxis = chart.getAxisRight();
rightAxis.setTypeface(tfLight);
rightAxis.setTextColor(Color.RED);
rightAxis.setAxisMaximum(900);
rightAxis.setAxisMinimum(200);
rightAxis.setDrawGridLines(false);
rightAxis.setDrawZeroLine(false);
rightAxis.setGranularityEnabled(false);
setData(4,30);
}
ArrayList<Entry> values3 = new ArrayList<>();
private void setData(int count, float range) {
float val= (float) (count*40+560);
values3.add(new Entry(count, val));
Collections.sort(values3,new EntryXComparator());
System.out.println("i&val:"+count+" "+val+" "+values3.toString());
LineDataSet set3;
set3 = new LineDataSet(values3, "DataSet 3");
set3.setAxisDependency(YAxis.AxisDependency.RIGHT);
set3.setColor(Color.YELLOW);
set3.setCircleColor(Color.WHITE);
set3.setLineWidth(2f);
set3.setCircleRadius(3f);
set3.setFillAlpha(65);
set3.setFillColor(ColorTemplate.colorWithAlpha(Color.YELLOW, 200));
set3.setDrawCircleHole(false);
set3.setHighLightColor(Color.rgb(244, 117, 117));
LineData data = new LineData(set3);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
chart.setData(data);
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set3 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set3.setValues(values3);
set3.notifyDataSetChanged();
chart.invalidate();
System.out.println("value:" + values3.toString());
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
chart.invalidate();
}
@Override
protected void saveToGallery() {
saveToGallery(chart, "LineChartActivity2");
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("Entry selected", e.toString());
chart.centerViewToAnimated(e.getX(), e.getY(), chart.getData().getDataSetByIndex(h.getDataSetIndex())
.getAxisDependency(), 500);
}
@Override
public void onNothingSelected() {
Log.i("Nothing selected", "Nothing selected.");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}
DemoBase.java,我是组件的demo里的源码。