/** * The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet. * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data. * * @param e * @return */ public void getBarBounds(BarEntry e, RectF outputRect) { RectF bounds = outputRect; IBarDataSet set = mData.getDataSetForEntry(e); if (set == null) { bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); return; } float y = e.getY(); float x = e.getX(); float barWidth = mData.getBarWidth(); float left = x - barWidth / 2f; float right = x + barWidth / 2f; float top = y >= 0 ? y : 0; float bottom = y <= 0 ? y : 0; bounds.set(left, top, right, bottom); getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect); }
@Override public void getBarBounds(BarEntry e, RectF outputRect) { RectF bounds = outputRect; IBarDataSet set = mData.getDataSetForEntry(e); if (set == null) { outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); return; } float y = e.getY(); float x = e.getX(); float barWidth = mData.getBarWidth(); float top = x - barWidth / 2f; float bottom = x + barWidth / 2f; float left = y >= 0 ? y : 0; float right = y <= 0 ? y : 0; bounds.set(left, top, right, bottom); getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); }
@Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { if (!mDrawWholeStack && entry instanceof BarEntry) { BarEntry barEntry = (BarEntry) entry; float[] vals = barEntry.getYVals(); if (vals != null) { // find out if we are on top of the stack if (vals[vals.length - 1] == value) { // return the "sum" across all stack values return mFormat.format(barEntry.getY()) + mAppendix; } else { return ""; // return empty } } } // return the "proposed" value return mFormat.format(value) + mAppendix; }
@SuppressLint("NewApi") @Override public void onValueSelected(Entry e, Highlight h) { if (e == null) return; RectF bounds = mOnValueSelectedRectF; mChart.getBarBounds((BarEntry) e, bounds); MPPointF position = mChart.getPosition(e, AxisDependency.LEFT); Log.i("bounds", bounds.toString()); Log.i("position", position.toString()); Log.i("x-index", "low: " + mChart.getLowestVisibleX() + ", high: " + mChart.getHighestVisibleX()); MPPointF.recycleInstance(position); }
protected BarData generateBarData(int dataSets, float range, int count) { ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>(); for(int i = 0; i < dataSets; i++) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); // entries = FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "stacked_bars.txt"); for(int j = 0; j < count; j++) { entries.add(new BarEntry(j, (float) (Math.random() * range) + range / 4)); } BarDataSet ds = new BarDataSet(entries, getLabel(i)); ds.setColors(ColorTemplate.VORDIPLOM_COLORS); sets.add(ds); } BarData d = new BarData(sets); d.setValueTypeface(tf); return d; }
/** * generates a random ChartData object with just one DataSet * * @return */ private BarData generateData(int cnt) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < 12; i++) { entries.add(new BarEntry(i, (float) (Math.random() * 70) + 30)); } BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt); d.setColors(ColorTemplate.VORDIPLOM_COLORS); d.setBarShadowColor(Color.rgb(203, 203, 203)); ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>(); sets.add(d); BarData cd = new BarData(sets); cd.setBarWidth(0.9f); return cd; }
@Override public void refreshContent(Entry e, Highlight highlight) { if (e instanceof BarEntry) { BarEntry be = (BarEntry) e; if(be.getYVals() != null) { // draw the stack value tvContent.setText("" + Utils.formatNumber(be.getYVals()[highlight.getStackIndex()], 0, true)); } else { tvContent.setText("" + Utils.formatNumber(be.getY(), 0, true)); } } else { tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true)); } super.refreshContent(e, highlight); }
/** * generates a random ChartData object with just one DataSet * * @return */ private BarData generateDataBar(int cnt) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < 12; i++) { entries.add(new BarEntry(i, (int) (Math.random() * 70) + 30)); } BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt); d.setColors(ColorTemplate.VORDIPLOM_COLORS); d.setHighLightAlpha(255); BarData cd = new BarData(d); cd.setBarWidth(0.9f); return cd; }
private void setData(int count) { ArrayList<BarEntry> yVals = new ArrayList<BarEntry>(); for (int i = 0; i < count; i++) { float val = (float) (Math.random() * count) + 15; yVals.add(new BarEntry(i, (int) val)); } BarDataSet set = new BarDataSet(yVals, "Data Set"); set.setColors(ColorTemplate.VORDIPLOM_COLORS); set.setDrawValues(false); BarData data = new BarData(set); mChart.setData(data); mChart.invalidate(); mChart.animateY(800); }
@SuppressLint("NewApi") @Override public void onValueSelected(Entry e, Highlight h) { if (e == null) return; RectF bounds = mOnValueSelectedRectF; mChart.getBarBounds((BarEntry) e, bounds); MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex()) .getAxisDependency()); Log.i("bounds", bounds.toString()); Log.i("position", position.toString()); MPPointF.recycleInstance(position); }
private BarData generateBarData(List<Integer> progressSoFar) { ArrayList<BarEntry> barEntries = new ArrayList<>(progressSoFar.size()); DateTime dateTime = new DateTime(); referenceTime = dateTime.minusDays(7).getMillis(); int totalSeconds = 0; for (int i = 0; i < progressSoFar.size(); i++) { int progress = progressSoFar.get(i); barEntries.add(new BarEntry(i, progress)); totalSeconds += progress; } activityTotal.setText(context.getString(R.string.total_project_time, FormatUtils.getFormattedTime(context, totalSeconds))); BarDataSet barDataSet = new BarDataSet(barEntries, ""); barDataSet.setColors(blue400); BarData barData = new BarData(barDataSet); barData.setBarWidth(0.85f); barData.setDrawValues(false); return barData; }
private BarData generateBarData(int dailyGoal, List<Integer> progressSoFar) { ArrayList<BarEntry> barEntries = new ArrayList<>(progressSoFar.size()); DateTime dateTime = new DateTime(); referenceTime = dateTime.minusDays(7).getMillis(); for (int i = 0; i < progressSoFar.size(); i++) { barEntries.add(new BarEntry(i, progressSoFar.get(i))); } BarDataSet barDataSet = new BarDataSet(barEntries, ""); int colors[] = new int[progressSoFar.size()]; for (int i = 0; i < progressSoFar.size(); i++) { colors[i] = (progressSoFar.get(i) >= dailyGoal * 60 * 60) ? green400 : red400; } barDataSet.setColors(colors); BarData barData = new BarData(barDataSet); barData.setBarWidth(0.85f); barData.setDrawValues(false); return barData; }
private void addChartEntry(HabitEvent event) { String date = event.getCompletedate().format(formatter); int index = dateMap.get(date); BarEntry entry = entries.get(index); float[] stacks = entry.getYVals(); String attribute = event.getHabitAttribute(); if (attribute.equals("Physical")) { stacks[0] += 1; } else if (attribute.equals("Mental")) { stacks[1] += 1; } else if (attribute.equals("Discipline")) { stacks[2] += 1; } else if (attribute.equals("Social")) { stacks[3] += 1; } entry.setVals(stacks); }
/**BarData chart items*/ private ChartItem buildAgeChartItem(SQLiteDatabase db, long period) { ChartItem result = null; Cursor cursor = buildCursorForCharts(DemographicsType.AGE, db, period); if (cursor != null) { List<BarEntry> entries = new ArrayList<>(); while (cursor.moveToNext()) { if (cursor.getInt(0) > 0){// age>0 float value = cursor.getFloat(0); int count = cursor.getInt(1); entries.add(new BarEntry(value, count, new EntryMarkerData(value, count))); } } if (!entries.isEmpty()){ BarData barData = ChartHelper.populateBarData(entries, "", null); result = new VerticalBarChartItem(context, barData); ((VerticalBarChartItem) result).setTitle(context.getString(R.string.chart_item_age)); } cursor.close(); } return result; }
private void init_chart_category() { barChart = (HorizontalBarChart) findViewById(R.id.show_chart_category); List<BarEntry> yVals1 = new ArrayList<BarEntry>(); List<BarEntry> yVals2 = new ArrayList<BarEntry>(); final JSONArray jsonarray = dbHelper.getCategoryProfitExpense(start, end); try { for (int i = 0; i < jsonarray.length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); String name = jsonobject.getString("name"); String profit = jsonobject.getString("profit"); String expense = jsonobject.getString("expense"); yVals1.add(new BarEntry(i, Math.abs(Float.valueOf(expense)))); yVals2.add(new BarEntry(i, Math.abs(Float.valueOf(profit)))); } } catch (JSONException e) { } createBarChart(yVals1, yVals2, jsonarray); }
public void prepareChartData(Hit hit){ entries = new ArrayList<BarEntry>(); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); entries.add(new BarEntry(0, (float) (hit.getFields().getNfCalories()/2000)*100)); entries.add(new BarEntry(1, (float) (hit.getFields().getNfTotalFat()/65)*100)); entries.add(new BarEntry(2, (float) (hit.getFields().getNfCholesterol()/300)*100)); entries.add(new BarEntry(3, (float)(hit.getFields().getNfSodium()/2400)*100)); entries.add(new BarEntry(4, (float)(hit.getFields().getNfTotalCarbohydrate()/300)*100)); entries.add(new BarEntry(5, (float) hit.getFields().getNfSodium())); entries.add(new BarEntry(6, (float) (hit.getFields().getNfProtein()/50)*100)); // entries.add(new BarEntry(7, (float) hit.getFields().getNfVitaminADv(), "(mg)")); // entries.add(new BarEntry(8, (float) hit.getFields().getNfVitaminCDv(), "(mg)")); // entries.add(new BarEntry(9, (float) hit.getFields().getNfCalciumDv(), "(mg)")); BarDataSet dataSet = new BarDataSet(entries, String.format("%s Nutrition", food_item).toUpperCase()); setChartData(dataSet); }
@Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { if (!mDrawWholeStack && entry instanceof BarEntry) { BarEntry barEntry = (BarEntry) entry; float[] vals = barEntry.getVals(); if (vals != null) { // find out if we are on top of the stack if (vals[vals.length - 1] == value) { // return the "sum" across all stack values return mFormat.format(barEntry.getVal()) + mAppendix; } else { return ""; // return empty } } } // return the "proposed" value return mFormat.format(value) + mAppendix; }
/** * 柱形图数据 */ private BarData generateBarData() { BarData barData = new BarData(); ArrayList<BarEntry> entries1 = new ArrayList<>(); for (int index = 0; index < items.size(); index++) { entries1.add(new BarEntry(index + 1f, (float) items.get(index).main_data.getData())); } BarDataSet barDataSet = new BarDataSet(entries1, "当前数据"); barDataSet.setValues(entries1); barDataSet.setDrawValues(false);//是否在线上显示值 barDataSet.setColor(Color.rgb(230, 230, 230)); barDataSet.setHighLightColor(Color.parseColor(items.get(dateSelected).state.getColor())); barDataSet.setValueTextColor(Color.rgb(60, 220, 78)); barDataSet.setValueTextSize(10f); barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT); float barWidth = 0.45f; barData.addDataSet(barDataSet); barData.setBarWidth(barWidth); return barData; }
/** * Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be * found in the charts data. * * @param e * @return */ public RectF getBarBounds(BarEntry e) { IBarDataSet set = mData.getDataSetForEntry(e); if (set == null) return null; float barspace = set.getBarSpace(); float y = e.getVal(); float x = e.getXIndex(); float barWidth = 0.5f; float spaceHalf = barspace / 2f; float left = x - barWidth + spaceHalf; float right = x + barWidth - spaceHalf; float top = y >= 0 ? y : 0; float bottom = y <= 0 ? y : 0; RectF bounds = new RectF(left, top, right, bottom); getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); return bounds; }
/** * Generate bar data bar data. * * @param dataSets the data sets * @param range the range * @param count the count * @return the bar data */ protected BarData generateBarData(int dataSets, float range, int count) { ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>(); for(int i = 0; i < dataSets; i++) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); // entries = FileUtils.loadEntriesFromAssets(getActivity().getAssets(), "stacked_bars.txt"); for(int j = 0; j < count; j++) { entries.add(new BarEntry(j, (float) (Math.random() * range) + range / 4)); } BarDataSet ds = new BarDataSet(entries, getLabel(i)); ds.setColors(ColorTemplate.VORDIPLOM_COLORS); sets.add(ds); } BarData d = new BarData(sets); d.setValueTypeface(tf); return d; }
/** * generates a random ChartData object with just one DataSet * * @return */ private BarData generateData(int cnt) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < 12; i++) { entries.add(new BarEntry((int) (Math.random() * 70) + 30, i)); } BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt); d.setBarSpacePercent(20f); d.setColors(ColorTemplate.VORDIPLOM_COLORS); d.setBarShadowColor(Color.rgb(203, 203, 203)); ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>(); sets.add(d); BarData cd = new BarData(getMonths(), sets); return cd; }
private void setData(int count) { ArrayList<String> xVals = new ArrayList<String>(); ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < count; i++) { xVals.add(i+""); entries.add(mSinusData.get(i)); } BarDataSet set = new BarDataSet(entries, "Sinus Function"); set.setBarSpacePercent(40f); set.setColor(Color.rgb(240, 120, 124)); BarData data = new BarData(xVals, set); data.setValueTextSize(10f); data.setValueTypeface(mTf); data.setDrawValues(false); mChart.setData(data); }
@Override public void refreshContent(Entry e, Highlight highlight) { if (e instanceof BarEntry) { BarEntry be = (BarEntry) e; if(be.getVals() != null) { // draw the stack value tvContent.setText("" + Utils.formatNumber(be.getVals()[highlight.getStackIndex()], 0, true)); } else { tvContent.setText("" + Utils.formatNumber(be.getVal(), 0, true)); } } else { tvContent.setText("" + Utils.formatNumber(e.getVal(), 0, true)); } }
/** * generates a random ChartData object with just one DataSet * * @return */ private BarData generateDataBar(int cnt) { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < 12; i++) { entries.add(new BarEntry((int) (Math.random() * 70) + 30, i)); } BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt); d.setBarSpacePercent(20f); d.setColors(ColorTemplate.VORDIPLOM_COLORS); d.setHighLightAlpha(255); BarData cd = new BarData(getMonths(), d); return cd; }
private void setData(int count) { ArrayList<BarEntry> yVals = new ArrayList<BarEntry>(); ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < count; i++) { float val = (float) (Math.random() * count) + 15; yVals.add(new BarEntry((int) val, i)); xVals.add((int) val + ""); } BarDataSet set = new BarDataSet(yVals, "Data Set"); set.setColors(ColorTemplate.VORDIPLOM_COLORS); set.setDrawValues(false); BarData data = new BarData(xVals, set); mChart.setData(data); mChart.invalidate(); mChart.animateY(800); }
/** * This method creates the Highlight object that also indicates which value of a stacked BarEntry has been selected. * * @param old * the old highlight object before looking for stacked values * @param set * @param xIndex * @param dataSetIndex * @param yValue * @return */ protected Highlight getStackedHighlight(Highlight old, IBarDataSet set, int xIndex, int dataSetIndex, double yValue) { BarEntry entry = set.getEntryForXIndex(xIndex); if (entry == null || entry.getVals() == null) return old; Range[] ranges = getRanges(entry); int stackIndex = getClosestStackIndex(ranges, (float) yValue); if(ranges.length > 0) return new Highlight(xIndex, dataSetIndex, stackIndex, ranges[stackIndex]); else return null; }
private void setData(int count, float range) { ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < count; i++) { xVals.add(mMonths[i % 12]); yVals1.add(new BarEntry((float) (Math.random() * range), i)); } BarDataSet set1 = new BarDataSet(yVals1, "DataSet 1"); ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); dataSets.add(set1); BarData data = new BarData(xVals, dataSets); data.setValueTextSize(10f); data.setValueTypeface(tf); mChart.setData(data); }
@Override public BarEntry buildEntryFromResultObject(T realmObject, int xIndex) { DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject); if (dynamicObject.getFieldType(mValuesField) == RealmFieldType.LIST) { RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField); float[] values = new float[list.size()]; int i = 0; for (DynamicRealmObject o : list) { values[i] = o.getFloat(mStackValueFieldName); i++; } return new BarEntry(values, mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField)); } else { float value = dynamicObject.getFloat(mValuesField); return new BarEntry(value, mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField)); } }
private BarData generateOrderBarData(List<MarketHistory> historyEntries) { SimpleDateFormat format = new SimpleDateFormat("MMM dd", Locale.getDefault()); int size = historyEntries.size(); List<BarEntry> entries = new ArrayList<>(size); List<String> xAxis = new ArrayList<>(size); for (int i = 0; i < size; i++) { MarketHistory history = historyEntries.get(i); Date recordDate = new Date(history.getRecordDate()); xAxis.add(format.format(recordDate)); entries.add(new BarEntry((float) history.getOrderCount(), i)); } BarDataSet set = new BarDataSet(entries, "Order Count"); set.setColor(Color.parseColor("#99FF99")); // set.setValueTextSize(0f); set.setDrawValues(false); set.setAxisDependency(YAxis.AxisDependency.RIGHT); return new BarData(xAxis, set); }
/** * Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be * found in the charts data. * * @param e * @return */ public RectF getBarBounds(BarEntry e) { BarDataSet set = mData.getDataSetForEntry(e); if (set == null) return null; float barspace = set.getBarSpace(); float y = e.getVal(); float x = e.getXIndex(); float barWidth = 0.5f; float spaceHalf = barspace / 2f; float left = x - barWidth + spaceHalf; float right = x + barWidth - spaceHalf; float top = y >= 0 ? y : 0; float bottom = y <= 0 ? y : 0; RectF bounds = new RectF(left, top, right, bottom); getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); return bounds; }
/** * Returns a list of average reaction times from database */ private List<BarEntry> getAverageValuesFromDB(Activity activity, String selectedOperationIssue, String gameType) { List<BarEntry> yValues = new ArrayList<>(); if (selectedOperationIssue != null) { double averagePreOperationValue = new ReactionGameManager(activity).getFilteredReactionGames(selectedOperationIssue, gameType, Type.getTestType(Type.TestTypes.PreOperation), "AVG"); double averageInOperationValue = new ReactionGameManager(activity).getFilteredReactionGames(selectedOperationIssue, gameType, Type.getTestType(Type.TestTypes.InOperation), "AVG"); double averagePostOperationValue = new ReactionGameManager(activity).getFilteredReactionGames(selectedOperationIssue, gameType, Type.getTestType(Type.TestTypes.PostOperation), "AVG"); // pre, in and post operation values yValues.add(new BarEntry(0, (float) averagePreOperationValue)); yValues.add(new BarEntry(1, (float) averageInOperationValue)); yValues.add(new BarEntry(2, (float) averagePostOperationValue)); yValues.add(new BarEntry(3, 0f)); // dummy } return yValues; }
/** * 生成柱状图的数据 */ private BarData generateDataBar() { ArrayList<BarEntry> entries = new ArrayList<BarEntry>(); for (int i = 0; i < 12; i++) { entries.add(new BarEntry((int) (Math.random() * 70) + 30, i)); } BarDataSet d = new BarDataSet(entries, "New DataSet "); // 设置柱状图之间的间距 d.setBarSpacePercent(20f); // 设置显示的柱状图的颜色 d.setColors(ColorTemplate.VORDIPLOM_COLORS); // 设置高亮的透明度:当点击柱状图时显示的透明度 d.setHighLightAlpha(255); BarData cd = new BarData(getMonths(), d); return cd; }
@Override void dataSetConfig(IDataSet<BarEntry> dataSet, ReadableMap config) { BarDataSet barDataSet = (BarDataSet) dataSet; ChartDataSetConfigUtils.commonConfig(barDataSet, config); ChartDataSetConfigUtils.commonBarLineScatterCandleBubbleConfig(barDataSet, config); if (BridgeUtils.validate(config, ReadableType.Number, "barSpacePercent")) { barDataSet.setBarSpacePercent((float) config.getDouble("barSpacePercent")); } if (BridgeUtils.validate(config, ReadableType.String, "barShadowColor")) { barDataSet.setBarShadowColor(Color.parseColor(config.getString("barShadowColor"))); } if (BridgeUtils.validate(config, ReadableType.Number, "highlightAlpha")) { barDataSet.setHighLightAlpha(config.getInt("highlightAlpha")); } if (BridgeUtils.validate(config, ReadableType.Array, "stackLabels")) { barDataSet.setStackLabels(BridgeUtils.convertToStringArray(config.getArray("stackLabels"))); } }