最近有个艰巨任务,需要换一个图表框架,现在比较常用的事MPandroidChat,但是性能损耗有点大,领导要求换一个性能好的,所以找到这个 SciChart
官方文档 https://www.scichart.com/documentation/android/v1.x/webframe.html#Creating%20your%20First%20SciChart%20Android%20App%20-%20Part%204.html
实现的比较简单,先试一下这个效果,熟悉一下API,后续会深入研究一下。
public class MainActivity extends AppCompatActivity {
SciChartSurface surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
SciChartBuilder.init(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface = (SciChartSurface) findViewById(R.id.chart);
initView();
}
private void initView() {
/**
* 必须将SciChartSurface指向许可文件的位置。该SciChartSurface类具有这种特殊的方法, setRuntimeLicenseKeyFromResource(...),这需要一个语境和许可文件作为参数的相对路径。
* 我们要调用它在MainActivity类别中的onCreate(...)方法的开始,刚刚创建SciChartSurface实例后:
*/
try {
surface.setRuntimeLicenseKeyFromResource(this,"app\\src\\mian\\res\\raw\\license.xml");
} catch (Exception e) {
e.printStackTrace();
}
/**
*
* 添加轴的SciChartSurface
*/
DateAxis xAxis = new DateAxis(this);
AxisCollection xAxes = surface.getXAxes();
xAxes.add(xAxis);
/**
* 设置一个标题在X轴上
*/
xAxis.setAxisTitle("X Axis");
/**
* Y轴可以被添加到SciChartSurface:
*/
NumericAxis yAxis = new NumericAxis(this);
AxisCollection yAxes = surface.getYAxes();
yAxes.add(yAxis);
/**
* 让我们配置Y轴出现在左边调用 setAxisAlignment(...)方法:
*/
yAxis.setAxisAlignment(AxisAlignment.Left);
/**
* 设置一个标题在Y轴上
*/
yAxis.setAxisTitle("Y Axis");
/**
* 添加RenerableSeries到SciChartSurface
* 一旦轴设置,我们要做出SciChartSurface画一条线。SciChart提供特殊的类被称为“ RenderableSeries ”是负责绘制不同的图表类型,
* 如线(FastLineRenderableSeries),列(FastColumnsRenderableSeries),烛台系列(FastCandlestickRenderableSeries),
* 填充区域(FastMountainRenderableSeries),热图(FastUniformHeatmapRenderableSeries),误差棒(FastErrorBarsRenderableSeries)等。
* 每个 RenderableSeries类型都有自己的特点,但也有很多,他们的共同点,如隐藏或显示的可能性 RenderableSeries或显示 PointMarkers在它的每一个点。
* 正如有人所说,我们将使用 FastLineRenderableSeries在本教程中。一旦线系列创建,它可以被添加到SciChartSurface的RenderableSeries集合:
*/
FastLineRenderableSeries lineRenderableSeries = new FastLineRenderableSeries();
RenderableSeriesCollection renderableSeries = surface.getRenderableSeries();
renderableSeries.add(lineRenderableSeries);
/**
* 再次,不要忘记导入所有的类。
* 正如你可能会注意到,SciChart提供了一个名为特殊的辅助类ColorUtil。
* 它有几个预定义的整数颜色常量,在大多数polular和广泛使用的颜色。
* 现在,我们应该告诉行系列使用此PenStyle为的StrokeStyle:
*
* 注意:这个方法没有测试出来有点问题,需要后续研究
*/
// float a[] = {0.1f,0.5f};
// PenStyle penStyle = new PenStyle(true,2f,a) {
// @Override
// public boolean isVisible() {
// return false;
// }
//
// @Override
// public int getColor() {
// return ColorUtil.Green;
// }
// };
//
// lineRenderableSeries.setStrokeStyle(penStyle);
/**
* 添加DataSeries到RenderableSeries
* 该生产线系列需要将得出了一些数据。为了获得最佳性能,SciChart提供了一系列特殊的类所谓的“ DataSeries其目的是为了有效地存储数据和访问它的快速和最佳的方式。”
* 最常用的 DataSeries类型是 XyDataSeries,其被用于存储X和Y值的两个集合。但是,它可能是不够的一些 RenderableSeries类型。
* 例如, FastCandlestickRenderableSeries需要四个值作为一个数据点(开放,高,低,关闭),而不是一个简单的X,Y对。因此,有一个特殊的 DataSeries称为类型 OhlcDataSeries。
* 在本教程中,我们将使用 XyDataSeries用于保存数据,因为它是 DataSeries型 FastLineRenderableSeries可以工作。
* 用于DataSeries的构造需要用于每个组件以传递一数据类型; 在这里,我们将使用日期为X和双人间Y值:
*/
XyDataSeries dataSeries = new XyDataSeries(Date.class,Double.class);
int size = 100;
DateValues xValues = new DateValues(size);
DoubleValues yValues = new DoubleValues(size);
/**
* 要添加的数据点到DataSeries,你应该调用 追加(...)方法就可以了。每DataSeries类型提供的它几个替代,从而允许添加一个单一的数据点或一束在一个时间。
* 为了获得最佳的性能,你应该尝试分批尽可能多的附加数据点,利用这些 追加(...)接受数组或集合覆盖。
* 为此,SciChart提供了一个名为特殊接口 IValues,允许快速访问数据,以实现自己的藏品。有几个默认实现基本类型,
* DoubleValues, FloatValues, IntegerValues, LongValues, ShortValues和 DateValues。用这些来避免原始类型的装箱/拆箱,从而enchanching你的图表的表现。
* 在本教程中,我们将演示如何使用 DoubleValues和 DateValues。首先,您需要初始化实例,传递所需的容量,以构造(可选):
*/
Random random = new Random();
for (double i = 0;i<size;++i){
long xValue = new Date().getTime()+ DateIntervalUtil.fromDays(i);
xValues.add(new Date(xValue));
yValues.add(random.nextDouble());
}
dataSeries.append(xValues,yValues);
lineRenderableSeries.setDataSeries(dataSeries);
surface.zoomExtents();
}
@Override
protected void onDestroy() {
super.onDestroy();
SciChartBuilder.dispose();
}
}