Skip to content

Instantly share code, notes, and snippets.

@tolotrasmile
Last active July 31, 2019 07:49
Show Gist options
  • Save tolotrasmile/dc90cb7b5d72e099e63dc0666b696049 to your computer and use it in GitHub Desktop.
Save tolotrasmile/dc90cb7b5d72e099e63dc0666b696049 to your computer and use it in GitHub Desktop.
const {
map,
prop,
compose,
pluck,
sort,
ascend,
multiply,
cond,
T,
identity,
is,
tail
} = require("ramda");
const data = require("./data/ads.json");
const configs = require("./configs");
const moment = require("moment");
const { format } = require("momento");
const isArray = Array.isArray || is(Array);
const always = (fn, defaultValue) =>
cond([[fn, identity], [T, () => defaultValue]]);
const alwaysArray = always(isArray, []);
const processColor = x => x;
function getConfig({ colors, ...config }) {
const normalizedColors = alwaysArray(colors);
const color = processColor(tail(normalizedColors));
return {
mode: "CUBIC_BEZIER",
drawValues: false,
lineWidth: 2,
circleColor: color,
drawCircles: true,
drawCircleHole: true,
circleRadius: 4,
highlightColor: processColor("transparent"),
color: color,
drawFilled: true,
fillGradient: {
colors: normalizedColors.map(processColor),
positions: normalizedColors.map((_, index, a) => index / (a.length - 1)),
angle: 90,
orientation: "TOP_BOTTOM"
},
fillAlpha: 200,
valueTextSize: 15,
...config
};
}
// Get sorted values
const getSortedValues = compose(
sort(ascend(prop("day"))),
pluck("@attributes"),
alwaysArray
);
// Format date array
const formatDate = dateFormat =>
compose(
format(dateFormat),
moment
);
// Map array to date labels array
const mapLabels = compose(
map(
compose(
formatDate("DD/MM"),
prop("day")
)
),
getSortedValues
);
// Map values from key and coefficient
const keyCoeff = ({ key, coefficient }) =>
compose(
map(
compose(
y => ({ y }),
multiply(coefficient),
prop(key)
)
),
getSortedValues
);
// Map config array to chart config
const mapConfigs = array => config => ({
...getConfig(config),
values: keyCoeff(config)(array)
});
map(mapConfigs(data))(configs);
mapLabels(data);
// console.log(mapLabels(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment