我有地图矢量。每个地图都有三个键:x,:y和:z。我希望:z的值是:x和:y的值的总和。我应该怎么做?即
private BarChart positiveChart;
ArrayList<BarEntry> value;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_que, container, false);
this.positiveChart = view.findViewById(R.id.positiveChart);
value = new ArrayList<>();
chartAdjust(); // it is optional for my required formatting
setValue();
}
private void chartAdjust() {
Description description = new Description();
description.setText("");
positiveChart.setMaxVisibleValueCount(100);
positiveChart.setFitBars(true);
positiveChart.setHighlightFullBarEnabled(false);
positiveChart.disableScroll();
positiveChart.setDescription(description);
positiveChart.getLegend().setEnabled(true);
positiveChart.setDoubleTapToZoomEnabled(false);
positiveChart.setScaleEnabled(false);
positiveChart.getAxisLeft().setDrawLabels(false);
positiveChart.getAxisRight().setDrawLabels(false);
positiveChart.getXAxis().setDrawLabels(false);
positiveChart.getAxisLeft().setDrawGridLines(false);
positiveChart.getAxisRight().setDrawGridLines(false);
positiveChart.getXAxis().setDrawGridLines(false);
positiveChart.setNoDataTextColor(Color.RED);
positiveChart.setBackgroundColor(Color.WHITE);
positiveChart.setDrawBorders(false);
positiveChart.getXAxis().setDrawAxisLine(false);
positiveChart.getAxisRight().setEnabled(false);
positiveChart.getAxisLeft().setEnabled(false);
}
public void setValue()
{
float maleOp1 = (float) (data.getAnswerOfManOption1());
float femaleOp1 = (float) (data.getAnswerOfWomanOption1());
xValue.add(new BarEntry(0, new float[]{maleOp1, femaleOp1}));
BarDataSet set1;
set1 = new BarDataSet(xValue, "");
set1.setDrawIcons(true);
set1.setStackLabels(new String[]{"Male", "Female"});
set1.setDrawValues(true);
set1.setValueTextSize(12f);
set1.setColors(ColorTemplate.JOYFUL_COLORS);
BarData data1 = new BarData(set1);
data1.setBarWidth(15f);
// And BarDataSet And BarData Is set as per your required then set the value in the Variable of barchart : see below you
positiveChart.setData(data1);
positiveChart.setFitBars(true);
positiveChart.invalidate();
}
在上面的示例中,第一个地图的:z值应为(+ 5 10)= 15。
感谢您的帮助!
答案 0 :(得分:1)
如果要在以后添加这些键,则必须查看如何操作地图。在这种情况下销毁和assoc
很好:
user=> (def m {:x 42 :y 666})
#'user/m
user=> (let [{:keys [x y]} m] (assoc m :z (+ x y)))
{:x 42, :y 666, :z 708}
或者,如果您想仅使用坐标来创建新地图,请为此编写一个函数
user=> (defn coord [x y] {:x x :y y :z (+ x y)})
#'user/coord
user=> (coord 42 666)
{:x 42, :y 666, :z 708}
答案 1 :(得分:0)
map
和for
将返回一个惰性序列,而mapv
将返回一个向量。一个简单的解决方案:
;; first we define the data
(def ms [{x: 5 :y 10} {:x 1 :y 1} {:x 2 :y 3}])
;; then we define a function that creates mutated data
(defn add-sum [ms] (mapv (fn [m] (assoc m :z (+ (:x m) (:y m)))) ms))
或者使用for
宏:
(defn add-sum [ms] (for [m ms] (assoc m :z (+ (:x m) (:y m)))))
或使用destructuring:
(defn add-sum [ms] (for [{:keys [x y]} ms] (assoc m :z (+ x y))))
答案 2 :(得分:0)
如果您打算重复执行此操作,则可以通过将其提取到函数中来参数化此逻辑,尽管我会在创建地图时创建:z
键,因为您大概会拥有当时可以访问x
和y
。
除非这是已经提出的优良解决方案的替代方案,只是针对不同的论点进行了概括。
(def ms [{:x 5 :y 10} {:x 5 :y 12} {:x 8 :y 10}])
(defn assoc-from-existing [m k & ks]
(assoc m k (->> (select-keys m ks) vals (apply +))))
(mapv #(assoc-from-existing % :z :x :y) ms)