I want to plot a XY line graph with some double values on Y axis and some String values on X axis. But the XYSeries takes only double/long values. So,is there any way I can use a string array on the X axis?
Thanks for any suggestions.
code:
//fDates are strings
XYSeries fPriceseries = new XYSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
long fDate = Long.parseLong(fDates[i]);
fPriceseries.add(fDate, fPrice[i]);
}
XYSeries fMileageSeries = new XYSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
long fDate = Long.parseLong(fDates[i]);
fMileageSeries.add(fDate, fMileage[i]);
}
Use TimeSeries:
fDates as Date array
TimeSeries fPriceseries = new TimeSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
fPriceseries.add(fDates[i], fPrice[i]);
}
TimeSeries fMileageSeries = new TimeSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
fMileageSeries.add(fDates[i], fMileage[i]);
}
fDates as String array - Using SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
TimeSeries fPriceseries = new TimeSeries("Fuel prices");
for(int i=0;i<fDates.length;i++)
{
fPriceseries.add(sdf.parse(fDates[i]), fPrice[i]);
}
TimeSeries fMileageSeries = new TimeSeries("Mileage");
for(int i=0;i<fDates.length;i++)
{
fMileageSeries.add(sdf.parse(fDates[i]), fMileage[i]);
}
Referencies:
http://www.achartengine.org/content/javadoc/org/achartengine/model/TimeSeries.html
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You cannot plot a graph using string on any axis.
Explanation:
When we plot a graph we compare two values... as in 3,5 or 7,22 or 12.3,10000..
Plotting string with respect to an integer or double,is like comparing apples and oranges.
So m sorry friend but its not possible.. unless you convert the string or create a reference for its contents..
Related
I've been trying to plot a graph from two ArrayLists on my Android Application. I got the values from ArrayList succeed, but I can't call method to get values.
I create this method which gets values from my database:
public DataPoint[] setData(String data)
{
TextView editText4;
editText4 = (TextView) findViewById(R.id.editText4);
String[] lucas = data.split(",");
List<String> ok = new ArrayList<String>(Arrays.asList(lucas));
List<String> yes = new ArrayList<String>(Arrays.asList(lucas));
int n = ok.size(); //to find out the no. of data-points
editText4.setText(String.valueOf(n));
DataPoint[] values = new DataPoint[n]; //creating an object of type DataPoint[] of size 'n'
for (int i = 0; i < n; i++)
{
DataPoint v = new DataPoint(Double.parseDouble(ok.get(i)), Double.parseDouble(yes.get(i)));
values[i] = v;
}
return values;
}
My problem is here in setData (Cannot Resolve symbol 'setData') :
GraphView graph;
PointsGraphSeries<DataPoint> series; //an Object of the PointsGraphSeries for plotting scatter graphs
graph = (GraphView) findViewById(R.id.graph);
series = new PointsGraphSeries<>(setData); //initializing/defining series to get the data from the method 'data()'
graph.addSeries(series); //adding the series to the GraphView
series.setShape(PointsGraphSeries.Shape.POINT);
series.setSize(6);
As looking for your code looks that you have method setData(String data) but in series = new PointsGraphSeries<>(setData); you are calling variable. Please assing result from setData(String data) method to local variable and then use it in mentioned line of code.
I created a Piechart with MPandroidchart for my app. The chart works great. The problem is that the original data contains 4 data entry but the chart only show 3. Can somebody help? What is the limitation for this?
My PieChart
Hi Thank you so much for getting me back. The xVals and yVals are from SQLite database. xVals{30-,30-45,46-60,60-} String and yVals(6,14,13,3) float.
My codes are posted here:
private void AddDataCYNewCustomersAge(){
DataBaseHelper databasehelper = new DataBaseHelper (NewCustomerReportCurrentYear.this);
PieChart ageChart = new PieChart(this);
ageChart = (PieChart)findViewById(R.id.agechart);
//get yVals and datasets of marriage chart
ArrayList<Entry> yValsageCounts = new ArrayList<Entry>();
for(int i = 0;i < databasehelper.yDataIncomeCounts().size(); i++)
yValsageCounts.add(new Entry(databasehelper.yDataAgeCounts().get(i), i));
//get xVals of marriage chart
ArrayList<String> xValsage = new ArrayList<String>();
for(int i = 0;i < databasehelper.xDataAge().size();i++)
xValsage.add(databasehelper.xDataAge().get(i));
PieDataSet ageCounts = new PieDataSet(yValsageCounts, "");
//set yvals attribute
ageCounts.setSliceSpace(3);
ageCounts.setSelectionShift(5);
//add many colors
ageCounts.setColors(new int[]{R.color.Fuchsia, R.color.Aqua,R.color.Yellow,R.color.LightSeaGreen,R.color.Blue}, this);
// configue pie chart
ageChart.setUsePercentValues(true);
ageChart.setRotationEnabled(true);
ageChart.setRotationAngle(0);
ageChart.setDescriptionPosition(350, 550);
ageChart.setDrawHoleEnabled(true);
ageChart.setHoleColor(Color.TRANSPARENT);
ageChart.setHoleRadius(13);
ageChart.setTransparentCircleRadius(18);
ageChart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
//ageChart.getLegend().setXEntrySpace(3);
// ageChart.getLegend().setYEntrySpace(3);
ageChart.getLegend().setTextColor(Color.BLUE);
ageChart.setDescription("Income");
ageChart.setDescriptionTextSize(13);
ageChart.setDescriptionColor(Color.BLUE);
//get income chart data
PieData ageData = new PieData(xValsage,ageCounts);
ageData.setValueFormatter(new PercentFormatter());
ageData.setValueTextSize(11);
ageData.setValueTextColor(Color.BLUE);
ageChart.setData(ageData);
//refresh data
ageChart.invalidate();
}
In my android application, I am new to graphs and using MPchart library to draw the graphs. I'm displaying the graph using string array on both - X axis and Y axis.
For x axis I created it successfully but for Y axis I need to convert my ArrayList<String> to BarData array list. I don't know how to do convert it. Can anyone tell me how to achieve this?
For X axis:
private ArrayList<String> getXAxis() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis = new ArrayList<String>(Arrays.asList(bar_graph_names)); //converting string to arraylist
return xAxis;
}
For Y axis plotting values:
private ArrayList<BarDataSet> getData() {
ArrayList<String> yAxis_conv = new ArrayList<>();
yAxis_conv = new ArrayList<String>(Arrays.asList(bar_graph_values));
ArrayList<BarDataSet> yAxis_Datasets=null;
ArrayList<BarEntry> yAxis = new ArrayList<>();
for(int i = 0; i < bar_graph_values.length; i++)
{
BarEntry [] barentry = new BarEntry[bar_graph_values.length];
barentry[i] = new BarEntry(Float.valueOf(yAxis_conv.get(i)),i);
}
BarDataSet barDataSet1 = new BarDataSet(yAxis_conv , "");//Error occurring line
barDataSet1.setColors(ColorTemplate.COLORFUL_COLORS);
yAxis_Datasets = new ArrayList<>();
yAxis_Datasets.add(barDataSet1);
return yAxis_Datasets;
}
BarData object (which you give to the chart View) consist of one List representing x-values and one or more BarDataSet objects representing y-values data. BarDataSet object has String name and List of y-values.
Your structure of this objects is simply wrong. (for starters yAxis_conv is List<String>, where BarDataSet requires List<BarEntry>). There's more mistakes, but i'm not gonna point them out, just have some working code below:
private void setData() {
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
xVals = new ArrayList<String>(Arrays.asList(bar_graph_names));
for(int i=0; i < bar_graph_values.length; i++) {
BarEntry entry = new BarEntry(Float.valueOf(bar_graph_values[i]), i);
yVals.add(entry);
}
BarDataSet newSet = new BarDataSet(yVals, "DataSet");
BarData data = new BarData(xVals, newSet);
mChart.setData(data);
}
I use AchartEngine to create a LineGraph.
I have the data from the database in the format:
dd-mm-yyyy count
for example:
01-05-2013 3
01-08-2013 7
01-11-2013 4
01-12-2013 15
...
code fragment
...
int values = new Date[myCursor.getCount()];
Date[] dat = new Date[myCursor.getCount()];
SimpleDateFormat dfIn = new SimpleDateFormat("yyyy-MM-dd");
do{
try {
dat[kk]=dfIn.parse(myCursor.getString(0));
values[kk]=myCursor.getInt(1);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kk++;
}
while(myCursor.moveToNext());
...
TimeSeries series = new TimeSeries("Line1");
for( int i = 0; i < dat.length; i++)
{
series.add(datki[i], values[i]);
}
I have a questions:
Dates and values I hold in separate tables.
How can I add to TimeSeries this data ???
Is it possible to add the date in the format mm-yyyy ???
Please help
If using chartfactory :
intent = ChartFactory.getTimeChartIntent(this, getDateDemoDataset(), getDemoRenderer(), "MM-yyyy");
If using timechart:
TimeChart chart = new TimeChart(dataset, renderer);
chart.setDateFormat("MM-yyyy");
I have a lineGraph with float values on both x-axis and y-axis? My question is that how can i set String values on x-axis like date or name of months.I am using GraphView-3.0.jar library.my code is
values_list.add(1.0);
values_list.add(5.0);
values_list.add(9.0);
values_list.add(12.0);
values_list.add(17.0);
values_list.add(1.0);
values_list.add(13.0);
values_list.add(23.0);
graphData = new GraphViewData[(values_list.size())];
double price_copy = 0;
for (int i = 0; i < values_list.size(); i++) {
price_copy = values_list.get(i);
graphData[i] = new GraphViewData(i, price_copy);
}
GraphView graphView;
graphView = new LineGraphView(this // context
, "" );// graphView.addSeries(exampleSeries);// data
graphView.addSeries(new GraphViewSeries("values",
new GraphViewStyle(Color.rgb(00, 00, 128), 3),
graphData));
graphView.setShowLegend(true);
// set view port, start=2, size=40
graphView.setViewPort(0, 10);
graphView.setScalable(true);
graphView.setScrollable(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.LNL_CHART);
layout.addView(graphView);
Also i have no idea about AchartEngine,thanks in advance.
Use achart engine library. you can search for that on google. it helped me.Thanks
Just use the label formatter. You could store your x-labels as string in an array.
For example:
final String[] xLabels = new String[] {
"foo", "bar", "third", "bla", "more"
};
GraphView graphView = new LineGraphView(this, "example") {
#Override
protected String formatLabel(double value, boolean isValueX) {
if (isValueX) {
return xLabels[(int) value];
} else {
// return y label as number
return super.formatLabel(value, isValueX); // let the y-value be normal-formatted
}
}
};