function createEvolutionChart_onTop(data, field, regionCode) {
const speciality = field.split("_")[1];
const isPressureChart = field.split("_")[0] === "pressure";
const isArea = data[0].data_type === "area";
let colorScale;
let rangePressure;
let rangeRatio;
let maxRatio;
let partialData;
const offset = 5;
if (speciality === "mf") {
colorScale = color_mf;
maxRatio = maxRatio_mf;
rangePressure = rangeFullSerie_Pressure_mf;
rangeRatio = rangeFullSerie_Ratio_mf;
partialData = isPressureChart ? `pressure_mf_partial` : "";
} else if (speciality === "enf") {
colorScale = color_enf;
maxRatio = maxRatio_enf;
rangePressure = rangeFullSerie_Pressure_enf;
rangeRatio = rangeFullSerie_Ratio_enf;
partialData = isPressureChart ? `pressure_enf_partial` : "";
} else if (speciality === "ped") {
colorScale = color_ped;
maxRatio = maxRatio_ped;
rangePressure = rangeFullSerie_Pressure_ped;
rangeRatio = rangeFullSerie_Ratio_ped;
partialData = isPressureChart ? `pressure_ped_partial` : "";
}
const range = isPressureChart ? rangePressure : rangeRatio;
const margin = marginChart;
const mainColorRatio = colorNeutral(800);
const currentArray = data.map((d) => d[field]);
const areAllNulls = currentArray.every((d) => typeof d !== "number");
const dataCleaned = data.filter(function (d) {
return typeof d[field] === "number";
});
const lastIndexNotNull = dataCleaned.findLastIndex((d) => d);
// SPECIAL CASE: NO PEDIATRIA IN some CSS
const noPediatriaCS =
isPressureChart &&
speciality === "ped" &&
currentArray[lastIndexNotNull] === "No tiene pediatría";
// Create SVG
const svg = d3
.create("svg")
.attr("class", "evolutionChart")
.attr("width", widthChart)
.attr("height", heightChart);
// Update domain Y scale
y.domain(range).range([heightChart - margin.bottom, margin.top]);
x.range([margin.left, widthChart - margin.right]);
if (!areAllNulls) {
// Show background charts
document.getElementById(
`${isPressureChart ? "pressure" : "ratio"}-chart_bckgnd-${speciality}`
).style.opacity = 1;
// Actual chart
svg
.append("path")
.datum(dataCleaned)
.attr("fill", "none")
.attr("stroke", isPressureChart ? colorNeutral(600) : colorNeutral(500))
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x((d) => x(d.date))
.y((d) => y(d[field]))
);
// Data points
svg
.selectAll("circle")
.data(dataCleaned)
.join("circle")
.attr("cx", (d) => x(d.date))
.attr("cy", (d) => y(d[field]))
.attr("r", function (d, i) {
const myRadius = i === lastIndexNotNull ? 4.2 : 3;
return myRadius;
})
.attr("fill", (d) =>
isPressureChart ? colorScale(d[field]) : mainColorRatio
);
if (isPressureChart) {
const partialNote = svg.append("g");
partialNote
.selectAll("text")
.data(dataCleaned)
.join("text")
.attr("class", "partial-data-note")
.attr("x", 5)
.attr("y", heightChart - 10)
.text((d) => d[partialData])
.style("font-weight", 600)
.style("fill", (d) => colorScale(d[field]));
}
const textChart = svg.append("g").attr("class", "text-chart");
textChart
.selectAll("text")
.data(dataCleaned)
.join("text")
.style("text-anchor", "middle")
.style("font-weight", (d, i) => (i === lastIndexNotNull ? 800 : 600))
.style("font-size", (d, i) =>
i === lastIndexNotNull ? "0.75rem" : "0.65rem"
)
.style("opacity", (d, i) => (i === lastIndexNotNull ? 1 : 0.8))
.attr("dy", -10)
.attr("x", (d) => x(d.date))
.attr("y", (d) => y(d[field]))
.text((d) =>
isPressureChart
? formatDecimal(d[field]) + (d[partialData] ? " (*)" : "")
: formatIntegers(d[field])
)
// To improve readability
// White background
.attr("stroke", colorNeutral(0))
.attr("stroke-width", 2.5)
// Black visible text
.clone(true)
.style("fill", (d) =>
isPressureChart ? colorScale(d[field]) : mainColorRatio
)
.attr("stroke", "none");
} else {
// Hide background charts
document.getElementById(
`${isPressureChart ? "pressure" : "ratio"}-chart_bckgnd-${speciality}`
).style.opacity = 0;
showNoDataInfo(
svg,
isPressureChart,
isArea,
speciality,
regionCode,
noPediatriaCS
);
}
return svg.node();
}