SlideShare a Scribd company logo
1 of 11
"Android Application Development Company India" www.letsnurture.com
Draw Graph using Chart engine
Library
"Android Application Development Company India" www.letsnurture.com
Draw Graph using Chart engine Library
Follow Simple steps to draw chart like
 line chart
 area chart
 scatter chart
 time chart
 bar chart
 pie chart
 bubble chart
 doughnut chart
 range (high-low) bar chart
 dial chart / gauge
 combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart
 cubic line chart
Link to Examle
Here example of Line Chart
step 1:
download A-Chart Engine library from this link. copy the jar file into the libs folder of your
project.
step 2
the activity_main.xml like the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
"Android Application Development Company India" www.letsnurture.com
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Chart_layout"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
step 3
In the main.java do the following steps.
// First Create a Graphical View object called mChart.
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.BasicStroke;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
"Android Application Development Company India" www.letsnurture.com
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private GraphicalView mChart;
private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
private void openChart() {
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
// int[] margin = { 10, -10, 10, 10 };
int[] expense = { 2200, 2200, 2200, 2200, 2200, 2200, 2200, 2200 };
// Creating an XYSeries for Performance
XYSeries performanceSeries = new XYSeries("Perfomance");
XYSeries expenseSeries = new XYSeries("expense");
// Adding data to Income and Expense Series
for (int i = 0; i < x.length; i++) {
performanceSeries.add(x[i], income[i]);
expenseSeries.add(x[i], expense[i]);
}
// Creating a dataset to hold each series
"Android Application Development Company India" www.letsnurture.com
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Perfomance Series to the dataset
dataset.addSeries(performanceSeries);
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize/style performanceSeries
XYSeriesRenderer performanceRenderer = new XYSeriesRenderer();
performanceRenderer.setColor(Color.GREEN);
performanceRenderer.setPointStyle(PointStyle.TRIANGLE);
performanceRenderer.setFillPoints(true);
performanceRenderer.setStroke(BasicStroke.SOLID);
performanceRenderer.setLineWidth(2);
performanceRenderer.setDisplayChartValues(true);
// Creating second XYSeriesRenderer to customize/style performanceSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.YELLOW);
expenseRenderer.setPointStyle(PointStyle.TRIANGLE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setStroke(BasicStroke.SOLID);
expenseRenderer.setLineWidth(2);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(-15);
multiRenderer.setLabelsTextSize(20);
multiRenderer.setAxisTitleTextSize(30);
// add chart title and x and y title
multiRenderer.setChartTitle("Performance Chart");
multiRenderer.setXTitle("Performance X title");
"Android Application Development Company India" www.letsnurture.com
multiRenderer.setYTitle("Performance Y Title");
// multiRenderer.setZoomButtonsVisible(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i + 1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to
// multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(performanceRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = (GraphicalView) ChartFactory.getLineChartView(
getBaseContext(), dataset, multiRenderer);
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Finally You will get a Line Chart that will look like this.
"Android Application Development Company India" www.letsnurture.com
Here example of Pie Chart
step 1:
create a new android activity and name it as PieChart.
Step 2:
Now, navigate to "/res/layout" and select the xml file associated with above activity. In this case,
activity_pie_chart.xml and paste the following code there.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/chart"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
step 3:
"Android Application Development Company India" www.letsnurture.com
Now, navigate to PieChart.java file and replace the code with below code
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class PieChart extends Activity {
int[] pieChartValues={25,15,20,40};
publics tatic final String TYPE = "type";
private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,
Color.MAGENTA, Color.CYAN };
private CategorySeries mSeries = new CategorySeries("");
private DefaultRenderer mRenderer = new DefaultRenderer();
"Android Application Development Company India" www.letsnurture.com
private GraphicalView mChartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pie_chart);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100,50,50,50));
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] {20,30,15,0});
mRenderer.setZoomButtonsVisible(true);
mRenderer.setStartAngle(90);
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
layout.addView(mChartView, new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
} else {
mChartView.repaint();
}
fillPieChart();
}
"Android Application Development Company India" www.letsnurture.com
public void fillPieChart(){
for(int i=0;i<pieChartValues.length;i++)
{
mSeries.add(" Share Holder "+ (mSeries.getItemCount() + 1),
pieChartValues[i]);
SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
renderer.setColor(COLORS[(mSeries.getItemCount() -1) %
COLORS.length]);
mRenderer.addSeriesRenderer(renderer);
if (mChartView != null)
mChartView.repaint();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The working of code is almost similar to that of Bar chart. Renderer is used to actually draw the Pie
chart and we created a Dataset as a sample array
"Android Application Development Company India" www.letsnurture.com
int[] pieChartValues={25,15,20,40};
Then, we declare the different colors which will be used the differentiate the graph area. In our case
private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE,
Color.MAGENTA, Color.CYAN };
Then we have to declare some basic characteristics for renderer such as title, title size, margin,
zoom option or start angle etc. You can always modify it as per requirements. Since we are using
another activity to display the Pie chart so we declare it in onCreate method of PieChart activity.
Then we actually create reference of the "chart" layout we created in step 4 and assign the
"chartView" to it and call the fillPieChart() method.
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(10);
layout.addView(mChartView, new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
} else {
mChartView.repaint();
}
fillPieChart();
}
Link of Source Code

More Related Content

Viewers also liked

Basic business statistics 2
Basic business statistics 2Basic business statistics 2
Basic business statistics 2Anwar Afridi
 
Workplace Engagement Survey - Presentation
Workplace Engagement Survey - PresentationWorkplace Engagement Survey - Presentation
Workplace Engagement Survey - PresentationKestly Development
 
How to Write a One-Page Abstract
How to Write a One-Page AbstractHow to Write a One-Page Abstract
How to Write a One-Page AbstractMindy McAdams
 
IELTS ACADEMIC TASK 1: How to describe a pie chart
IELTS ACADEMIC TASK 1: How to describe a pie chartIELTS ACADEMIC TASK 1: How to describe a pie chart
IELTS ACADEMIC TASK 1: How to describe a pie chartBen Worthington
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wantedKetan Raval
 
материал к тесту 6 класс
материал к тесту 6 классматериал к тесту 6 класс
материал к тесту 6 классkryljanauki
 
Holes (themes)
Holes (themes)Holes (themes)
Holes (themes)salmaowen
 
Marshall Cassidy : VOCALSpin : My Dream Girl
Marshall Cassidy : VOCALSpin : My Dream GirlMarshall Cassidy : VOCALSpin : My Dream Girl
Marshall Cassidy : VOCALSpin : My Dream GirlVOCAL SPIN
 
Инвестиции в 9 шагов
Инвестиции в 9 шаговИнвестиции в 9 шагов
Инвестиции в 9 шаговAleksei Shevchuk
 
Changing the culture of it
Changing the culture of itChanging the culture of it
Changing the culture of itMatt Mansell
 
Pil-educational art software designed by Miss Munrakhan
Pil-educational art software designed by Miss MunrakhanPil-educational art software designed by Miss Munrakhan
Pil-educational art software designed by Miss Munrakhanbindiya14
 

Viewers also liked (13)

Basic business statistics 2
Basic business statistics 2Basic business statistics 2
Basic business statistics 2
 
Workplace Engagement Survey - Presentation
Workplace Engagement Survey - PresentationWorkplace Engagement Survey - Presentation
Workplace Engagement Survey - Presentation
 
How to Write a One-Page Abstract
How to Write a One-Page AbstractHow to Write a One-Page Abstract
How to Write a One-Page Abstract
 
IELTS ACADEMIC TASK 1: How to describe a pie chart
IELTS ACADEMIC TASK 1: How to describe a pie chartIELTS ACADEMIC TASK 1: How to describe a pie chart
IELTS ACADEMIC TASK 1: How to describe a pie chart
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wanted
 
материал к тесту 6 класс
материал к тесту 6 классматериал к тесту 6 класс
материал к тесту 6 класс
 
Dna
DnaDna
Dna
 
Holes (themes)
Holes (themes)Holes (themes)
Holes (themes)
 
Marshall Cassidy : VOCALSpin : My Dream Girl
Marshall Cassidy : VOCALSpin : My Dream GirlMarshall Cassidy : VOCALSpin : My Dream Girl
Marshall Cassidy : VOCALSpin : My Dream Girl
 
Final
FinalFinal
Final
 
Инвестиции в 9 шагов
Инвестиции в 9 шаговИнвестиции в 9 шагов
Инвестиции в 9 шагов
 
Changing the culture of it
Changing the culture of itChanging the culture of it
Changing the culture of it
 
Pil-educational art software designed by Miss Munrakhan
Pil-educational art software designed by Miss MunrakhanPil-educational art software designed by Miss Munrakhan
Pil-educational art software designed by Miss Munrakhan
 

More from Ketan Raval

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Ketan Raval
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Ketan Raval
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is hereKetan Raval
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyondKetan Raval
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected worldKetan Raval
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKetan Raval
 
Android notifications
Android notificationsAndroid notifications
Android notificationsKetan Raval
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantKetan Raval
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changerKetan Raval
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyKetan Raval
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsKetan Raval
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guideKetan Raval
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integrationKetan Raval
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google WayKetan Raval
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS applicationKetan Raval
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS appKetan Raval
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputingKetan Raval
 
All about Apple Watchkit
All about Apple WatchkitAll about Apple Watchkit
All about Apple WatchkitKetan Raval
 
How to upload application on iTune store
How to upload application on iTune storeHow to upload application on iTune store
How to upload application on iTune storeKetan Raval
 

More from Ketan Raval (20)

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
 
Keynote 2016
Keynote 2016Keynote 2016
Keynote 2016
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is here
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyond
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected world
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG Ahmedabad
 
Android notifications
Android notificationsAndroid notifications
Android notifications
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA Compliant
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changer
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gps
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guide
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integration
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google Way
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS application
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS app
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputing
 
All about Apple Watchkit
All about Apple WatchkitAll about Apple Watchkit
All about Apple Watchkit
 
How to upload application on iTune store
How to upload application on iTune storeHow to upload application on iTune store
How to upload application on iTune store
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Graph for Pie chart and Line Chart in Android Development

  • 1. "Android Application Development Company India" www.letsnurture.com Draw Graph using Chart engine Library
  • 2. "Android Application Development Company India" www.letsnurture.com Draw Graph using Chart engine Library Follow Simple steps to draw chart like  line chart  area chart  scatter chart  time chart  bar chart  pie chart  bubble chart  doughnut chart  range (high-low) bar chart  dial chart / gauge  combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart  cubic line chart Link to Examle Here example of Line Chart step 1: download A-Chart Engine library from this link. copy the jar file into the libs folder of your project. step 2 the activity_main.xml like the following: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
  • 3. "Android Application Development Company India" www.letsnurture.com android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/Chart_layout" android:orientation="vertical"> </LinearLayout> </LinearLayout> step 3 In the main.java do the following steps. // First Create a Graphical View object called mChart. import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.chart.PointStyle; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYSeries; import org.achartengine.renderer.BasicStroke; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu;
  • 4. "Android Application Development Company India" www.letsnurture.com import android.widget.LinearLayout; public class MainActivity extends Activity { private GraphicalView mChart; private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openChart(); } private void openChart() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8 }; int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 }; // int[] margin = { 10, -10, 10, 10 }; int[] expense = { 2200, 2200, 2200, 2200, 2200, 2200, 2200, 2200 }; // Creating an XYSeries for Performance XYSeries performanceSeries = new XYSeries("Perfomance"); XYSeries expenseSeries = new XYSeries("expense"); // Adding data to Income and Expense Series for (int i = 0; i < x.length; i++) { performanceSeries.add(x[i], income[i]); expenseSeries.add(x[i], expense[i]); } // Creating a dataset to hold each series
  • 5. "Android Application Development Company India" www.letsnurture.com XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); // Adding Perfomance Series to the dataset dataset.addSeries(performanceSeries); dataset.addSeries(expenseSeries); // Creating XYSeriesRenderer to customize/style performanceSeries XYSeriesRenderer performanceRenderer = new XYSeriesRenderer(); performanceRenderer.setColor(Color.GREEN); performanceRenderer.setPointStyle(PointStyle.TRIANGLE); performanceRenderer.setFillPoints(true); performanceRenderer.setStroke(BasicStroke.SOLID); performanceRenderer.setLineWidth(2); performanceRenderer.setDisplayChartValues(true); // Creating second XYSeriesRenderer to customize/style performanceSeries XYSeriesRenderer expenseRenderer = new XYSeriesRenderer(); expenseRenderer.setColor(Color.YELLOW); expenseRenderer.setPointStyle(PointStyle.TRIANGLE); expenseRenderer.setFillPoints(true); expenseRenderer.setStroke(BasicStroke.SOLID); expenseRenderer.setLineWidth(2); expenseRenderer.setDisplayChartValues(true); // Creating a XYMultipleSeriesRenderer to customize the whole chart XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer(); multiRenderer.setXLabels(-15); multiRenderer.setLabelsTextSize(20); multiRenderer.setAxisTitleTextSize(30); // add chart title and x and y title multiRenderer.setChartTitle("Performance Chart"); multiRenderer.setXTitle("Performance X title");
  • 6. "Android Application Development Company India" www.letsnurture.com multiRenderer.setYTitle("Performance Y Title"); // multiRenderer.setZoomButtonsVisible(true); for (int i = 0; i < x.length; i++) { multiRenderer.addXTextLabel(i + 1, mMonth[i]); } // Adding incomeRenderer and expenseRenderer to multipleRenderer // Note: The order of adding dataseries to dataset and renderers to // multipleRenderer // should be same multiRenderer.addSeriesRenderer(performanceRenderer); multiRenderer.addSeriesRenderer(expenseRenderer); // Getting a reference to LinearLayout of the MainActivity Layout LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container); // Creating a Line Chart mChart = (GraphicalView) ChartFactory.getLineChartView( getBaseContext(), dataset, multiRenderer); // Adding the Line Chart to the LinearLayout chartContainer.addView(mChart); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } Finally You will get a Line Chart that will look like this.
  • 7. "Android Application Development Company India" www.letsnurture.com Here example of Pie Chart step 1: create a new android activity and name it as PieChart. Step 2: Now, navigate to "/res/layout" and select the xml file associated with above activity. In this case, activity_pie_chart.xml and paste the following code there. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/chart" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> step 3:
  • 8. "Android Application Development Company India" www.letsnurture.com Now, navigate to PieChart.java file and replace the code with below code import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.model.CategorySeries; import org.achartengine.renderer.DefaultRenderer; import org.achartengine.renderer.SimpleSeriesRenderer; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; public class PieChart extends Activity { int[] pieChartValues={25,15,20,40}; publics tatic final String TYPE = "type"; private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN }; private CategorySeries mSeries = new CategorySeries(""); private DefaultRenderer mRenderer = new DefaultRenderer();
  • 9. "Android Application Development Company India" www.letsnurture.com private GraphicalView mChartView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pie_chart); mRenderer.setApplyBackgroundColor(true); mRenderer.setBackgroundColor(Color.argb(100,50,50,50)); mRenderer.setChartTitleTextSize(20); mRenderer.setLabelsTextSize(15); mRenderer.setLegendTextSize(15); mRenderer.setMargins(new int[] {20,30,15,0}); mRenderer.setZoomButtonsVisible(true); mRenderer.setStartAngle(90); if (mChartView == null) { LinearLayout layout = (LinearLayout) findViewById(R.id.chart); mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer); mRenderer.setClickEnabled(true); mRenderer.setSelectableBuffer(10); layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } else { mChartView.repaint(); } fillPieChart(); }
  • 10. "Android Application Development Company India" www.letsnurture.com public void fillPieChart(){ for(int i=0;i<pieChartValues.length;i++) { mSeries.add(" Share Holder "+ (mSeries.getItemCount() + 1), pieChartValues[i]); SimpleSeriesRenderer renderer = new SimpleSeriesRenderer(); renderer.setColor(COLORS[(mSeries.getItemCount() -1) % COLORS.length]); mRenderer.addSeriesRenderer(renderer); if (mChartView != null) mChartView.repaint(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } The working of code is almost similar to that of Bar chart. Renderer is used to actually draw the Pie chart and we created a Dataset as a sample array
  • 11. "Android Application Development Company India" www.letsnurture.com int[] pieChartValues={25,15,20,40}; Then, we declare the different colors which will be used the differentiate the graph area. In our case private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN }; Then we have to declare some basic characteristics for renderer such as title, title size, margin, zoom option or start angle etc. You can always modify it as per requirements. Since we are using another activity to display the Pie chart so we declare it in onCreate method of PieChart activity. Then we actually create reference of the "chart" layout we created in step 4 and assign the "chartView" to it and call the fillPieChart() method. if (mChartView == null) { LinearLayout layout = (LinearLayout) findViewById(R.id.chart); mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer); mRenderer.setClickEnabled(true); mRenderer.setSelectableBuffer(10); layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); } else { mChartView.repaint(); } fillPieChart(); } Link of Source Code