• Stars
    star
    124
  • Rank 288,207 (Top 6 %)
  • Language
    Java
  • License
    Other
  • Created over 7 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android wrapper for Highcharts usage

Highcharts

Highcharts Android is a delightful wrapper of HighchartsJS for Android.

The most popular, robust and battle-tested JavaScript Charting library is now available for Android with our new Java wrapper. Get gorgeous, multi-touch charts with minimal effort.

Documentation

Access the full API documentation here.

HOWTO

Here we present how to create basic chart and place it in your project.

What we need to do

  • Prepare your project for Highcharts
  • Create chart view and place it in your view
  • Create chart options and add them to your chart view
  • Run your app and enjoy!

Preparing your project

  • First of all download the Highcharts framework.

A) You can add the library to the gradle dependecies from Bintray:

Add the JitPack repository to your root build.gradle file:

repositories {
	maven { url 'https://jitpack.io' }
}

and following to the dependencies in your app build.gradle file:

dependencies {
    implementation 'com.github.highcharts:highcharts-android:10.2.0'
}

B) You can download the aar from here and add it manually if you want. Put the aar in the libs folder in your project structure:

Project structure screenshot

Then, add the following lines to your build.gradle file in repositories:

repositories {
    flatDir {
        dirs 'libs'
    }
}

and following to the dependencies in your app build.gradle file:

dependencies {
    compile (name: 'highcharts-release', ext:'aar')
    compile 'com.google.code.gson:gson:2.8.1'
}

You are now set to use Highcharts!

Using Highcharts (demo app)

Prepare the layout

At first, you need to create a view for your chart. Go to your activity_main.xml and add this to your layout:

<com.highsoft.highcharts.core.HIChartView
   android:id="@+id/hc"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

Add HIChartView to your Activity

In your MainActivity.java import Highcharts at the top

import com.highsoft.highcharts.core.*;
import com.highsoft.highcharts.common.HIChartsClasses.*;

Next, in onCreate add the HIChartView following line:

HIChartView chartView = (HIChartView) findViewById(R.id.hc);

This will create our chartView with the size defined in layout.

Done! Now let's create a chart!

Create the chart

Let's start with creating simple chart!

For the purpose of this tutorial, we will create a simple column chart using random data.

The heart of a chart is HIOptions class which contains all the information needed to present it. Some of the options there are optional, some are not (see demo app HighFit provided by Highcharts).

Create instance of HIOptions class

HIOptions options = new HIOptions();

Now we need to add the options that our chart requires to be presented. Let's start with chart type. To do so, create HIChart class object and set its type to "column"

HIChart chart = new HIChart();
chart.setType("column");

Add this object to your options

options.setChart(chart);

Then let's give our chart a name (title) and also add it to options

HITitle title = new HITitle();
title.setText("Demo chart");

options.setTitle(title);

Now we need to add some data (in this tutorial it will be some random set of numbers). Since we are creating a column chart, we need to use HIColumn data series

HIColumn series = new HIColumn();

To add data, just create an ArrayList with our data objects

series.setData(new ArrayList<>(Arrays.asList(49.9, 71.5, 106.4, 129.2, 144, 176, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4)));

Since options can store multiple series, we need to add our series as one-element ArrayList

options.setSeries(new ArrayList<HISeries>(Collections.singletonList(series)));

And at last add our options to the chartView

chartView.setOptions(options);

That's it! We are now set to run our application! Your MainActivity.java file should look like this

package com.highsoft.highchartsdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.highsoft.highcharts.core.*;
import com.highsoft.highcharts.common.HIChartsClasses.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        HIChartView chartView = (HIChartView) findViewById(R.id.hc);

        HIOptions options = new HIOptions();

        HIChart chart = new HIChart();
        chart.setType("column");
        options.setChart(chart);

        HITitle title = new HITitle();
        title.setText("Demo chart");

        options.setTitle(title);

        HIColumn series = new HIColumn();
        series.setData(new ArrayList<>(Arrays.asList(49.9, 71.5, 106.4, 129.2, 144, 176, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4)));
        options.setSeries(new ArrayList<HISeries>(Collections.singletonList(series)));

        chartView.setOptions(options);
    }
}

Press "Run" in Android Studio.

For more complex solutions see demo app HighFit provided by Highcharts or read the following documentation!

Additional info

Export module requirements

Please note that if you plan to use export module, you need to put specific provider in your app manifest:

<provider android:authorities="com.your.package.name.FileProvider"
   android:name="android.support.v4.content.FileProvider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
       android:name="android.support.FILE_PROVIDER_PATHS"
       android:resource="@xml/provider_paths"/>
</provider>

and the needed provider_paths file

<?xml version="1.0" encoding="utf-8"?>
<paths>
   <files-path name="export" path="." />
</paths>

into xml folder here:

XML folder location

HIColor example

Highcharts Android wrapper provides its own colors implementation. As you can notice, some options are of HIColor type. You can instantiate the desired color in few ways which are described in the API documentation. In here, we will show the most complex case which is gradient usage. First, you need to create the HIGradient (you can use base constructor with default values too):

HIGradient gradient = new HIGradient(0, 0.5f, 1, 1);

Next, you should define HIStop's:

LinkedList<HIStop> stops = new LinkedList<>();
stops.add(new HIStop(0.4f, HIColor.initWithRGB(160, 160, 160)));
stops.add(new HIStop(1, HIColor.initWithRGB(60, 60, 60)));

Now you can instantiate a color thanks to those objects for, let's say, chart background:

HIChart chart = new HIChart();
chart.setBackgroundColor(HIColor.initWithLinearGradient(gradient, stops));

HIFunction example

Thanks to Highcharts Android wrapper you can now assign native Android callbacks to on click events for specific chart elements. We will show you a small taste of such usage. For these purpose we will let appear a simple Toast with point coordinates when it's clicked but keep in mind that you can achieve much more with HIFunction mechanism!

First of all, you need to create a series:

HISpline spline = new HISpline();
spline.setData(new ArrayList<>(Arrays.asList(0.3,5.3,8.0,9.1,3.2,5.4,4.0,4.2,2.1,10.0)));

Now, you can refer to the point event and add on click callback like this:

spline.setPoint(new HIPoint());
spline.getPoint().setEvents(new HIEvents());
spline.getPoint().getEvents().setClick(new HIFunction(
        f -> {
            Toast t = Toast.makeText(
                this,
                "Clicked point [ " + f.getProperty("x") + ", " + f.getProperty("y") + " ]",
                Toast.LENGTH_SHORT);
                t.show();
            },
            new String[] {"x", "y"}
));

As you can see in the above code snippet first argument of the HIFunction is the actual callback defined in the lambda expression. Second argument is simple array of chart elements. We need to put them here to let wrapper pull them for us during HIFunction instantiation. Thanks to this, we can refer to these elements corresponding values by getProperty() method. You can pull any data from chart like this. Depending on the current needs you can just run some code, withdraw data from chart, return a String to the chart (e.g. in HITooltip formatter) and even put pure Javascript function in the constructor in the String format. To get more complex structures from HIFuncion refer to nested elements using dot e.g. yAxis.0.min. For more information feel free to check the API documentation.

Custom fonts

Highcharts Android wrapper allows you to add custom fonts. If you have your own font and want to use that in your chart follow next steps: Add a font file to your project. Select File -> Add Files to β€œYour Project Name” from the menu bar or drag and drop the file into your project.

  • Add a font file to your project.

Fonts location

  • Add your font to HIChartView. To do this, pass your font resource to your HIChartView:
HIChartView chartView = findViewById(R.id.hc);
chartView.addFont(R.font.griphite);

So, now you can use a custom font in your chart. For example, let's change the chart title font. You only need to create a style object for the title and set its font family to the font file name:

HICSSObject style = new HICSSObject();
style.setFontFamily("griphite");
HITitle title = new HITitle();
title.setStyle(style);

More Repositories

1

highcharts

Highcharts JS, the JavaScript charting framework
TypeScript
12,020
star
2

highcharts-react

The official Highcharts supported wrapper for React
JavaScript
978
star
3

highcharts-vue

Vue
646
star
4

highcharts-angular

Highcharts official integration for Angular
TypeScript
408
star
5

node-export-server

Highcharts Node.js export server
JavaScript
337
star
6

highcharts-editor

JavaScript
249
star
7

highcharts-dist

Official shim repo for Highcharts releases
JavaScript
214
star
8

highcharts-export-server

The exporting server allows users to send charts in SVG or JSON and let them convert and download as pdf, png, jpeg or a svg vector image.
Java
138
star
9

highcharts-ios

iOS wrapper for Highcharts.
JavaScript
125
star
10

highcharts-react-native

JavaScript
103
star
11

export-csv

Highcharts plugin to export chart data to CSV
JavaScript
76
star
12

highstock-release

Official shim repo for Highstock releases
JavaScript
53
star
13

draggable-points

JavaScript
32
star
14

map-collection-dist

HTML
26
star
15

rounded-corners

JavaScript
25
star
16

pattern-fill

JavaScript
21
star
17

highmaps-release

Official shim repo for Highmaps releases
JavaScript
19
star
18

highcharts-declarations-beta

Beta for Highcharts TypeScript declarations
JavaScript
11
star
19

sonification-studio

Vue
9
star
20

api-docs

Generator for API documentation based on output from the Highcharts JSDoc plugin
CSS
6
star
21

draggable-legend

JavaScript
6
star
22

adapt-chart-to-legend

JavaScript
6
star
23

github.highcharts.com

Node.js server which runs a RESTful application to serve Highcharts scripts built from the Highcharts build script.
JavaScript
6
star
24

dashboards-dist

Official shim repo for releases of Highcharts Dashboards
JavaScript
5
star
25

value-in-legend

JavaScript
5
star
26

highcharts-utils

JavaScript
5
star
27

highcharts-documentation-generators

Documentation generators
CSS
4
star
28

map-from-svg

SVG map parser for Highcharts Maps
JavaScript
3
star
29

highcharts-assembler

JavaScript
2
star
30

Highcharts-Cloud-API-Demo

JavaScript
2
star
31

highcharts-meteor

Meteor wrapper for Highcharts
JavaScript
2
star
32

highcharts-jsdom

A utility to convert Highcharts configs to SVGs without a browser
JavaScript
1
star
33

screen-dashboard

An application for our monitors.
HTML
1
star
34

Highcharts-Cloud-API-Demo-MEAN-Stack

JavaScript
1
star
35

trackball

A plugin for Highcharts adding trackball/marker features to each series in the chart.
JavaScript
1
star
36

highcharts-declarations-generator

Generator scripts for generating TypeScript declarations
TypeScript
1
star