项目作者: swapnil1104

项目描述 :
A highly customizable and performant custom view to render curved line graph.
高级语言: Java
项目地址: git://github.com/swapnil1104/CurveGraphView.git
创建时间: 2020-01-04T09:21:42Z
项目社区:https://github.com/swapnil1104/CurveGraphView

开源协议:Apache License 2.0

下载


CurveGraphView



Android Arsenal

A highly customizable and performant custom view to render curved line graph.

Animation demo
Animation with straight & curved
Animation with opaque color
Animated and non animated graph
Horizontal guidelines

Packed with features

  • Add multiple line graphs within one graph plane.
  • Extensible styling options.
  • Performant and light weight.

How to integrate the library in your app?

Step 1: Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. maven { url "https://jitpack.io" }
  4. }
  5. }

Step 2. Add the dependency

  1. dependencies {
  2. implementation 'com.github.swapnil1104:CurveGraphView:{current_lib_ver}'
  3. }

Step 3. Add CurveGraphView to your layout file

  1. <com.broooapps.graphview.CurveGraphView
  2. android:id="@+id/cgv"
  3. android:layout_width="0dp"
  4. android:layout_height="250dp"
  5. app:layout_constraintBottom_toBottomOf="parent"
  6. app:layout_constraintEnd_toEndOf="parent"
  7. app:layout_constraintStart_toStartOf="parent"
  8. app:layout_constraintTop_toTopOf="parent" ></com.broooapps.graphview.CurveGraphView>

How to customize the view.

  1. curveGraphView = findViewById(R.id.cgv);
  2. curveGraphView.configure(
  3. new CurveGraphConfig.Builder(this)
  4. .setAxisColor(R.color.Blue) // Set number of values to be displayed in X ax
  5. .setVerticalGuideline(4) // Set number of background guidelines to be shown.
  6. .setHorizontalGuideline(2)
  7. .setGuidelineColor(R.color.Red) // Set color of the visible guidelines.
  8. .setNoDataMsg(" No Data ") // Message when no data is provided to the view.
  9. .setxAxisScaleTextColor(R.color.Black) // Set X axis scale text color.
  10. .setyAxisScaleTextColor(R.color.Black) // Set Y axis scale text color
  11. .setAnimationDuration(2000) // Set Animation Duration
  12. .build()
  13. );

How to provide data to the view.

Create PointMap object

The graph view points the plot with keeping 2 values in mind, span and value
span relates to the x-coordinate, and value relates to the y-coordinate.
Create the object by providing values as shown below.

  1. PointMap pointMap = new PointMap();
  2. pointMap.addPoint(0, 100);
  3. pointMap.addPoint(1, 500);
  4. pointMap.addPoint(5, 800);
  5. pointMap.addPoint(4, 600);

Create GraphData object for each PointMap

A GraphData object expects a PointMap, strokeColor of the graph, and an optional gradientColor.
Create a GraphData object as shown below.

  1. GraphData gd = GraphData.builder(this)
  2. .setPointMap(pointMap) // PointMap datqa
  3. .setGraphStroke(R.color.Black) // Graph line stroke color
  4. .setGraphGradient(R.color.BlueViolet, R.color.RoyalBlue) // Graph fill gradient color
  5. .setStraightLine(true) // true for straight line; false for curved line graph
  6. .setPointRadius(10) // set point radius
  7. .setPointColor(R.color.Red) // set point color
  8. .animateLine(true) // Trigger animation for the particular graph line!
  9. .build();

Provide the array of GraphData to CurveGraphView

Provide the above constructed data to CurveGraphView via the curveGraphView.setData(int span, int maxVal, GraphData… gds) method.
dscription of the params:

  • span: is the range from 0… i.e. this is the range of x-axis.
  • maxVal: is the maximum plottable value for Y axis.
  • gds… : is the array of GraphData objects.

Sample Code

  1. curveGraphView = findViewById(R.id.cgv);
  2. curveGraphView.configure(
  3. new CurveGraphConfig.Builder(this)
  4. .setAxisColor(R.color.Blue) // Set X and Y axis line color stroke.
  5. .setIntervalDisplayCount(7) // Set number of values to be displayed in X ax
  6. .setGuidelineCount(2) // Set number of background guidelines to be shown.
  7. .setGuidelineColor(R.color.GreenYellow) // Set color of the visible guidelines.
  8. .setNoDataMsg(" No Data ") // Message when no data is provided to the view.
  9. .setxAxisScaleTextColor(R.color.Black) // Set X axis scale text color.
  10. .setyAxisScaleTextColor(R.color.Black) // Set Y axis scale text color
  11. .setAnimationDuration(2000) // Set animation duration to be used after set data.
  12. .build()
  13. );
  14. PointMap pointMap = new PointMap();
  15. pointMap.addPoint(0, 100);
  16. pointMap.addPoint(1, 500);
  17. pointMap.addPoint(4, 600);
  18. pointMap.addPoint(5, 800);
  19. GraphData gd = GraphData.builder(this)
  20. .setPointMap(pointMap)
  21. .setGraphStroke(R.color.Black)
  22. .setGraphGradient(R.color.BlueViolet, R.color.RoyalBlue)
  23. .build();
  24. PointMap p2 = new PointMap();
  25. p2.addPoint(0, 140);
  26. p2.addPoint(1, 700);
  27. p2.addPoint(2, 100);
  28. p2.addPoint(3, 0);
  29. p2.addPoint(4, 190);
  30. GraphData gd2 = GraphData.builder(this)
  31. .setPointMap(p2)
  32. .setGraphStroke(R.color.Green)
  33. .setGraphGradient(R.color.gradientStartColor, R.color.gradientEndColor)
  34. .build();
  35. //TODO(Swapnil) Optimize the setting logic code.
  36. /** This needs to be done, onMeasure of Layout isn't called if setData is called in onCreate
  37. * If anyone can take this up as their first issue, it'd be great!
  38. */
  39. new Handler().postDelayed(new Runnable() {
  40. @Override
  41. public void run() {
  42. curveGraphView.setData(5, 1000, gd, gd2);
  43. }
  44. }, 250);