How to draw a polygon with androidPlot library? - android

I'm trying to draw a polygon with AndroidPlot library.
I'm successfully drawing the four points, but it draws only 3 sides of the polygon. (it's not closing the polygon)
The polygon should be filled. How can I achieve this?
Thanks!
Edit:
I'm firstly drawing a point and then the polygon, because I have to show if that point will be inside the polygon.
private XYPlot plot;
plot = (XYPlot) findViewById(R.id.graph);
seriesPointFormat = new LineAndPointFormatter(
null, // line color
Color.rgb(5, 100, 150), // point color
null, // fill color
new PointLabelFormatter());
float[] xTol = intent.getExtras().getFloatArray("tolerancesX");
float[] yTol = intent.getExtras().getFloatArray("tolerancesY");
Float[] xTolerances = new Float[xTol.length];
Float[] yTolerances = new Float[yTol.length];
for(int i = 0; i< xTol.length; i++)
xTolerances[i] = xTol[i];
for(int i = 0; i< yTol.length; i++)
yTolerances[i] = yTol[i];
//Example
x=0.35f;
y=0.65f;
//Turn the above arrays into XYSeries':
XYSeries seriesPoint = new SimpleXYSeries(
Arrays.asList(x),
Arrays.asList(y),
"point"); // Set the display title of the series
plot.getGraphWidget().setDomainValueFormat(new DecimalFormat("##.###"));
// add a new series' to the xyplot:
plot.addSeries(seriesPoint, seriesPointFormat);
XYSeries seriesPolygon = new SimpleXYSeries(
Arrays.asList(xTolerances),
Arrays.asList(yTolerances),
"Polygon");
LineAndPointFormatter seriesPolygonFormat = new LineAndPointFormatter(
Color.rgb(500, 0, 0), // line color
Color.rgb(500, 100, 0), // point color
null,
new PointLabelFormatter());
seriesPolygonFormat.getLinePaint().setStrokeWidth(2);
plot.addSeries(seriesPolygon, seriesPolygonFormat);

The answer is obvious, stupid me...(Facepalm)
You just have to add the first point at the end of the array.
Float[] xTolerances = new Float[xTol.length+1];
Float[] yTolerances = new Float[yTol.length+1];
for(int i = 0; i< xTol.length; i++)
xTolerances[i] = xTol[i];
//add here the first x coordinate at the end of the array
xTolerances[xTol.length] = xTol[0];
for(int i = 0; i< yTol.length; i++)
yTolerances[i] = yTol[i];
//add here the first x coordinate at the end of the array
yTolerances[yTol.length] = yTol[0];
And to fill in the polygon:
LineAndPointFormatter seriesPolygonFormat = new LineAndPointFormatter(
Color.rgb(500, 0, 0), // line color
Color.rgb(500, 100, 100), // point color
Color.argb(50, 200, 0, 0), //fill color with alpha, to make the fill transparent
new PointLabelFormatter());
I will not cancel the question, maybe will be useful to someone.

Related

AChartEngine Custom Axis

ScreenShot
Hello, i have a trouble with bar chart, i must remove this sticks on X and Y axis above numbers but i don't know how to do this, here is my code for creating chart: `String[] monthDays = new String[lastDayOfMonth];
// Create month days labels
int[] x = new int[lastDayOfMonth];
for (int i = 0; i < monthDays.length; i++) {
monthDays[i] = "" + (i + 1);
x[i] = i + 1;
}
// Create series
XYSeries majorSeries = new XYSeries("Major");
XYSeries middleSeries = new XYSeries("Middle");
XYSeries minorSeries = new XYSeries("Minor");
XYSeries restSeries = new XYSeries("Rest");
// Add data to series
for(int i = 0; i < x.length; i++) {
majorSeries.add(i, majorData[i]);
middleSeries.add(i, middleData[i]);
minorSeries.add(i, minorData[i]);
restSeries.add(i, restData[i]);
}
// Create series data set
XYMultipleSeriesDataset xyMultipleSeriesDataset = new XYMultipleSeriesDataset();
// Add series to data set
xyMultipleSeriesDataset.addSeries(restSeries);
xyMultipleSeriesDataset.addSeries(minorSeries);
xyMultipleSeriesDataset.addSeries(middleSeries);
xyMultipleSeriesDataset.addSeries(majorSeries);
// Create series render for major data set
XYSeriesRenderer majorXySeriesRenderer = new XYSeriesRenderer();
majorXySeriesRenderer.setColor(Color.parseColor("#4797b4"));
majorXySeriesRenderer.setFillPoints(true);
majorXySeriesRenderer.setLineWidth(2);
// Create series render for middle data set
XYSeriesRenderer middleXySeriesRenderer = new XYSeriesRenderer();
middleXySeriesRenderer.setColor(Color.parseColor("#4ec697"));
middleXySeriesRenderer.setFillPoints(true);
middleXySeriesRenderer.setLineWidth(2);
// Create series render for minor data set
XYSeriesRenderer minorXySeriesRenderer = new XYSeriesRenderer();
minorXySeriesRenderer.setColor(Color.parseColor("#1fb1ba"));
minorXySeriesRenderer.setFillPoints(true);
minorXySeriesRenderer.setLineWidth(2);
// Create series render for rest data set
XYSeriesRenderer restXySeriesRenderer = new XYSeriesRenderer();
restXySeriesRenderer.setColor(Color.parseColor("#c2c2c2"));
restXySeriesRenderer.setFillPoints(true);
restXySeriesRenderer.setLineWidth(2);
// Creating XYMultipleSeriesRenderer to customize chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setOrientation(XYMultipleSeriesRenderer.Orientation.HORIZONTAL);
// Add labels to x
for(int i = 0; i < monthDays.length; i++) {
multiRenderer.addXTextLabel(i, monthDays[i]);
}
multiRenderer.setXLabels(0);
multiRenderer.setYLabels(0);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setLabelsTextSize(24);
multiRenderer.setZoomButtonsVisible(false);
multiRenderer.setPanEnabled(true, false);
multiRenderer.setPanLimits(new double[] {-0.75, 31, 0, 0});
multiRenderer.setClickEnabled(false);
multiRenderer.setZoomEnabled(false, false);
multiRenderer.setShowGridY(false);
multiRenderer.setShowGridX(false);
multiRenderer.setFitLegend(true);
multiRenderer.setShowAxes(false);
multiRenderer.setShowLegend(false);
multiRenderer.setShowGrid(false);
multiRenderer.setZoomEnabled(false);
multiRenderer.setExternalZoomEnabled(false);
multiRenderer.setAntialiasing(true);
multiRenderer.setInScroll(true);
multiRenderer.setLegendHeight(30);
multiRenderer.setXLabelsAlign(Paint.Align.CENTER);
multiRenderer.setYLabelsAlign(Paint.Align.LEFT);
multiRenderer.setTextTypeface("sans_serif", Typeface.NORMAL);
multiRenderer.setYAxisMax(maxValue + (maxValue * 2 / 100));
multiRenderer.addYTextLabel(0, currency + "0.0");
if (maxValue == 0) {
multiRenderer.setYAxisMax(500);
multiRenderer.addYTextLabel(500, currency + maxValue);
} else {
multiRenderer.addYTextLabel(multiRenderer.getYAxisMax(), currency + maxValue);
}
multiRenderer.setXAxisMin(-0.5);
multiRenderer.setXAxisMax(11);
multiRenderer.setBarSpacing(0.5);
multiRenderer.setBackgroundColor(Color.TRANSPARENT);
multiRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setMargins(new int[]{30, 30, 30, 30});
// Add all renders to multi render
multiRenderer.addSeriesRenderer(restXySeriesRenderer);
multiRenderer.addSeriesRenderer(minorXySeriesRenderer);
multiRenderer.addSeriesRenderer(middleXySeriesRenderer);
multiRenderer.addSeriesRenderer(majorXySeriesRenderer);
// Create array of chart types
CombinedXYChart.XYCombinedChartDef[] types = new CombinedXYChart.XYCombinedChartDef[]{
new CombinedXYChart.XYCombinedChartDef(BarChart.TYPE, 0),
new CombinedXYChart.XYCombinedChartDef(BarChart.TYPE, 1),
new CombinedXYChart.XYCombinedChartDef(BarChart.TYPE, 2),
new CombinedXYChart.XYCombinedChartDef(BarChart.TYPE, 3)};
// Get view of chart
GraphicalView graphicalView = ChartFactory.getCombinedXYChartView(getContext(), xyMultipleSeriesDataset, multiRenderer, types);
chart.removeAllViews();
chart.addView(graphicalView);`
Solved
To do this you can set labels color same as your background or transparent
multiRenderer.setXLabelsColor(Color.WHITE);
multiRenderer.setYLabelsColor(0, Color.WHITE);
After this you have to create your own class that extends chart class witch you need
public class MyChart extends CombinedXYChart {
int color;
public MyChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, XYCombinedChartDef[] chartDefinitions) {
super(dataset, renderer, chartDefinitions);
}
#Override
protected void drawString(Canvas canvas, String text, float x, float y, Paint paint) {
paint.setColor(color);
super.drawString(canvas, text, x, y, paint);
}
public void setColor(int color) {
this.color = color;
}}
And finally you have to use your own class to create graphicalView and place it in layout
MyChart myChart = new MyChart(xyMultipleSeriesDataset, multiRenderer, types);
myChart.setColor(Color.BLACK);
GraphicalView graphicalView = new GraphicalView(getContext(), myChart);
This steps will override method from AbstractChart class witch draws only text values in your chart, but sticks will remain same color as your background, so they would be invisible

MPAndroidChart BubbleChart: single entry dataset ignores bubble size

I am using MPAndroidChart. I have an array of 5: float[] sizes that will dynamically get values ranged 0-1.
I need to draw 0-5 bubbles (won't draw if size <0.5) with a different color each. Since we can specify color only at Dataset-level not entry-level, I have to use single-entry data sets.
Below is my code. The issue is, they are all of size 1, disregarding the size they got on the fly (0.9f, 0.8f, 0.7f...).
How to solve this problem?
private void initChart(){
mChart = (BubbleChart) findViewById(R.id.chart);
//mChart.setDescription("");
mChart.setDrawGridBackground(false);
mChart.setTouchEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setMaxVisibleValueCount(5);
mChart.setPinchZoom(true);
mChart.getXAxis().setEnabled(false);
mChart.getAxisLeft().setEnabled(false);
mChart.getAxisRight().setEnabled(false);
mChart.setDescription("");
mChart.getXAxis().setAxisMinValue(-0.5f);
mChart.getXAxis().setAxisMaxValue(5.5f);
}
private void updateChart(){
ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
for(int i = 0, pos = 0; i < 5; ++i) {
if(sizes[i] < 0.5f)
continue;
BubbleEntry entry = new BubbleEntry(pos++, 0, sizes[i]); //!!These sizes e.g. 0.9f, 0.7f ... are disregarded
ArrayList<BubbleEntry> vals = new ArrayList<>();
vals.add(entry);
BubbleDataSet set = new BubbleDataSet(vals, "");
set.setColor(ColorTemplate.COLORFUL_COLORS[i], 130);
set.setDrawValues(false);
dataSets.add(set);
}
BubbleData data = new BubbleData(dataSets);
data.setDrawValues(false);
data.setHighlightCircleWidth(1.5f);
mChart.setData(data);
mChart.invalidate();
}
Solved. Add below line after new BubbleDataSet:
set.setNormalizeSizeEnabled(false);

AchartEngine Barchart Spacing not working when orientation is vertical and number of elements are 2

I am having a AchartEngine Barchart. I have only two bars to show with Orientation as Vertical. The space between the two bars is very high and I am not able to change it. I have tried the following
1. Change the values passed to setBarSpacing() method from -100, -10, -.9, -.5, -.1, 0, .1, .5, .9, 10, 100
2. Used two XYSeries and then one. In both cases, the space is significant
3. Increased and decreased width of Bar
4. Set the min, max and values at different ranges. min = .9, max = 2.1, x1 = 1, x2 = 2
Nothing is working. Can some one please help me with this?
Try the following method.
The method below adds a barchart showing data for two months with values. If you want to change the color and some other properties, you are free to do that.
private void createChart() {
String[] mMonth = new String[] { "Jan", "Feb" };
int[] mData = new int[] { 20, 25 };
// creating XY series for Data
XYSeries series = new XYSeries("Performance");
// fill the data
for (int i = 0; i < mMonth.length; i++) {
series.add(i, mData[i]);
}
// create a dataset to hold each data series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding dataseries to datast
dataset.addSeries(series);
// create a renderer to customize the series
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.GREEN);
renderer.setFillPoints(true);
renderer.setLineWidth(2);
renderer.setDisplayChartValues(true);
renderer.setChartValuesTextSize(30);
// create a renderer to customize entire chart
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setXLabels(0);
mRenderer.setXTitle("Quarter 1");
mRenderer.setYTitle("Values in K");
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.WHITE);
mRenderer.setMarginsColor(Color.WHITE);
mRenderer.setZoomButtonsVisible(false);
mRenderer.setZoomEnabled(false, false);
mRenderer.setPanEnabled(false, false);
// margins
int marginT = 50;
int marginL = 80;
int marginB = 15;
int marginR = 15;
mRenderer.setMargins(new int[] { marginT, marginL, marginB, marginR });
mRenderer.setShowLegend(false);
mRenderer.setAxisTitleTextSize(30);
// mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(30);
mRenderer.setLegendTextSize(30);
// mRenderer.addXTextLabel(8, "OT");
mRenderer.setXAxisMin(-0.5);
mRenderer.setXAxisMax(3);
mRenderer.setYAxisMin(0);
mRenderer.setYLabelsAlign(Align.RIGHT);
mRenderer.setXLabelsColor(Color.BLACK);
mRenderer.setYLabelsColor(0, Color.BLACK);
mRenderer.setXLabels(0);
mRenderer.setBarSpacing(2);
// We want to avoid black border
// transparent margins
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
for (int i = 0; i < mMonth.length; i++) {
mRenderer.addXTextLabel(i, mMonth[i]);
}
// adding renderer to multiple renderer(chart)
mRenderer.addSeriesRenderer(renderer);
GraphicalView view = ChartFactory.getBarChartView(this, dataset,
mRenderer, Type.STACKED);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rlBarChart);
layout.addView(view, 0);
}
Hope this helps.

Androidplot - background and ranges

I'm new to that library. I'm trying to Create the following plot:
Meanwhile I have the next one:
My questions: NOTE: The colors doesn't matters
How do I get rid of the black area (where was lables on range domain and title)
and center the plot (like in the first picture)
How to add ranges as in the first picture ? (x:[1-7] y:[0-4500])
Make the same grid as in the first picture
My code:
public class MainActivity extends Activity {
private XYPlot mySimpleXYPlot;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a couple arrays of y-values to plot:
Number[] days = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217 };
// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
mySimpleXYPlot.getBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.setBorderStyle(XYPlot.BorderStyle.NONE, null, null);
mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
// Domain
mySimpleXYPlot.getGraphWidget().setDomainLabelPaint(null);
mySimpleXYPlot.getGraphWidget().setDomainOriginLinePaint(null);
mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, days.length);
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("0"));
//Range
mySimpleXYPlot.getGraphWidget().setRangeOriginLinePaint(null);
mySimpleXYPlot.setRangeStep(XYStepMode.SUBDIVIDE, values.length);
mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0"));
//Remove legend
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getLegendWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getDomainLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getRangeLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getTitleWidget());
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(days),
Arrays.asList(values),
"Series1"); // Set the display title of the series
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.CYAN); // fill color
// setup our line fill paint to be a slightly transparent gradient:
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));
series1Format.setFillPaint(lineFill);
// add a new series' to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}
}
I hope this is helpful.
My code:
private XYPlot mySimpleXYPlot;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a couple arrays of y-values to plot:
Number[] days = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217 };
// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
mySimpleXYPlot.setBorderStyle(Plot.BorderStyle.NONE, null, null);
mySimpleXYPlot.setPlotMargins(0, 0, 0, 0);
mySimpleXYPlot.setPlotPadding(0, 0, 0, 0);
mySimpleXYPlot.setGridPadding(0, 10, 5, 0);
mySimpleXYPlot.setBackgroundColor(Color.WHITE);
mySimpleXYPlot.position(
mySimpleXYPlot.getGraphWidget(),
0,
XLayoutStyle.ABSOLUTE_FROM_LEFT,
0,
YLayoutStyle.RELATIVE_TO_CENTER,
AnchorPosition.LEFT_MIDDLE);
mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.getGraphWidget().getDomainLabelPaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getRangeLabelPaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getDomainOriginLabelPaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
mySimpleXYPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);
// Domain
mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, days.length);
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("0"));
mySimpleXYPlot.setDomainStepValue(1);
//Range
mySimpleXYPlot.setRangeBoundaries(0, 4500, BoundaryMode.FIXED);
mySimpleXYPlot.setRangeStepValue(10);
//mySimpleXYPlot.setRangeStep(XYStepMode.SUBDIVIDE, values.length);
mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0"));
//Remove legend
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getLegendWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getDomainLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getRangeLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getTitleWidget());
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(days),
Arrays.asList(values),
"Series1"); // Set the display title of the series
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.CYAN); // fill color
// setup our line fill paint to be a slightly transparent gradient:
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));
series1Format.setFillPaint(lineFill);
// add a new series' to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}
}
It will look like this:
In the latest version, instead of using
mySimpleXYPlot.position(
mySimpleXYPlot.getGraphWidget(),
0,
XLayoutStyle.ABSOLUTE_FROM_LEFT,
0,
YLayoutStyle.RELATIVE_TO_CENTER,
AnchorPosition.LEFT_MIDDLE);
it is to be used:
mySimpleXYPlot.getGraphWidget().setSize(new SizeMetrics(
0, SizeLayoutType.FILL,
0, SizeLayoutType.FILL));
This will eliminate the black area.
This will work for new Devices:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
plot.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.CYAN);
Argument is missing
It should be like this
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.rgb(0, 200, 0),Color.rgb(0, 100, 0),Color.CYAN, new PointLabelFormatter());

AChartEngine Multiple Y Axis overlapping Values

i use the AChartEngine as a library to draw my line Charts. This Line-Chart consists of Multiple Y-Axis (3-5 Axis) and goes on a Timeline on the X-Axis.
My Problem is, that Y-Axis-Values are overlapping (see image)
Heres my code:
//Puls, Geschwindigkeit und Hoehenmeter dem Diagramm hinzufugen
HashMap<Long, Heartrate> heartrateMap = trainingLog.getHeartrateList();
HashMap<Long, GPS> gpsMap = trainingLog.getGpsList();
//Puls, Geschwindigkeit und Hoehenmeter dem Diagramm hinzufugen
XYSeries heartrateSeries = new XYSeries("Puls", 0);
XYSeries altitudeSeries = new XYSeries("Höhenmeter", 1);
XYSeries speedSeries = new XYSeries("Geschwindigkeit", 2);
for(int i=0; i<trainingLog.getDuration(); i++){
Heartrate heartrate = heartrateMap.get(Long.valueOf(i));
if(heartrate != null){
heartrateSeries.add(i, heartrate.getHeartrate());
}
GPS gps = gpsMap.get(Long.valueOf(i));
if(gps != null){
altitudeSeries.add(i, gps.getAltitude());
speedSeries.add(i, gps.getSpeed());
}
}
XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();
dataSet.addSeries(0, heartrateSeries);
dataSet.addSeries(1, altitudeSeries);
dataSet.addSeries(2, speedSeries);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer(3);
//Renderer fuer Pulswerte
XYSeriesRenderer rendererHeartrate = new XYSeriesRenderer();
rendererHeartrate.setColor(Color.RED);
multiRenderer.addSeriesRenderer(rendererHeartrate);
//Renderer fuer Hoehenmeter
XYSeriesRenderer rendererAltitude = new XYSeriesRenderer();
rendererAltitude.setColor(Color.BLUE);
multiRenderer.addSeriesRenderer(rendererAltitude);
//Renderer fuer Geschwindigkeit
XYSeriesRenderer rendererSpeed = new XYSeriesRenderer();
rendererSpeed.setColor(Color.DKGRAY);
multiRenderer.addSeriesRenderer(rendererSpeed);
//Hintergrundfarbe weiß setzen
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
//Label--Einstellungen
multiRenderer.setYLabelsColor(0, Color.RED);
multiRenderer.setYLabelsColor(1, Color.BLUE);
multiRenderer.setYLabelsColor(2, Color.DKGRAY);
multiRenderer.setLabelsTextSize(25);
int length = multiRenderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = (XYSeriesRenderer) multiRenderer.getSeriesRendererAt(i);
r.setLineWidth(2f);
}
//Legenden-Einstellungen
multiRenderer.setLegendTextSize(20);
multiRenderer.setMargins(new int[] {20, 20, 20, 20});
Looper.prepare();
graphicalView = ChartFactory.getLineChartView(getActivity(), dataSet, multiRenderer);
Does anyone know how to solve this problem? The best way would be to set the y-labels parallel to each other, but i havent found a way to do this. Or is there a function to set the margin of each y-label to the y-axis?
Thanks for your help!
You could set different alignments for your Y axis labels. For instance, the first series would align the labels on the left, the second one on the right and the third on center.
multiRenderer.setYLabelsAlign(Align.LEFT, 0);
multiRenderer.setYLabelsAlign(Align.RIGHT, 1);
multiRenderer.setYLabelsAlign(Align.CENTER, 2);
Simply set y axis minimum value and maximum value for all three scales you used.
Then change color of two Y scales to transparent using Color.argb

Categories

Resources