Skip to content

Commit

Permalink
charts label fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Minotriz02 authored and Minotriz02 committed Nov 14, 2024
1 parent 0e7a9ba commit cd0b726
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
11 changes: 0 additions & 11 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"axios": "^1.7.7",
"bootstrap": "^5.3.3",
"chart.js": "^4.4.4",
"jquery": "^3.7.1",
"leaflet": "^1.9.4",
"react": "^18.3.1",
"react-bootstrap": "^2.10.4",
Expand Down
47 changes: 43 additions & 4 deletions src/src/components/weatherChart/WeatherChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ const WeatherChart = ({
title,
data,
unit,
chartOptions,
chartConfig,
color,
isChartLoading,
startAtZero,
}) => {
const maxItem = useMemo(
() =>
Expand Down Expand Up @@ -72,6 +71,44 @@ const WeatherChart = ({
document.body.removeChild(link);
};

const chartConfig = (label, data, color) => ({
labels: data.map((item) => item.label),
datasets: [
{
label,
data: data.map((item) => item.value),
fill: false,
borderColor: color,
tension: 0.1,
},
],
});

const chartOptions = useMemo(
() => ({
scales: {
x: { title: { display: true, text: "Días" } },
y: { title: { display: true, text: unit }, beginAtZero: false },
},
}),
[]
);

const ChartOptionsStartedAtZero = useMemo(
() => ({
...chartOptions,
scales: {
...chartOptions.scales,
y: {
...chartOptions.scales.y,
beginAtZero: true,
min: 0,
},
},
}),
[chartOptions]
);

return (
<Col lg={6} className="mb-4">
<div className="bg-white rounded p-4 text-dark h-100">
Expand Down Expand Up @@ -140,8 +177,10 @@ const WeatherChart = ({
identificar patrones climáticos recientes en la región.
</p>
<Line
data={chartConfig(title, data, color)}
options={chartOptions}
data={chartConfig("Datos estación", data, color)}
options={
startAtZero ? ChartOptionsStartedAtZero : chartOptions
}
/>
</>
)}
Expand Down
47 changes: 1 addition & 46 deletions src/src/pages/dashboard/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,44 +225,6 @@ const Dashboard = () => {

const compareData = () => {};

const chartConfig = (label, data, color) => ({
labels: data.map((item) => item.label),
datasets: [
{
label,
data: data.map((item) => item.value),
fill: false,
borderColor: color,
tension: 0.1,
},
],
});

const chartOptions = useMemo(
() => ({
scales: {
x: { title: { display: true, text: "Días" } },
y: { title: { display: true, text: "Valores" }, beginAtZero: false },
},
}),
[]
);

const precipitationChartOptions = useMemo(
() => ({
...chartOptions,
scales: {
...chartOptions.scales,
y: {
...chartOptions.scales.y,
beginAtZero: true,
min: 0,
},
},
}),
[chartOptions]
);

return (
<div className="bg-dashboard">
<Container className="p-3 bg-dashboard">
Expand Down Expand Up @@ -366,17 +328,14 @@ const Dashboard = () => {
title="Precipitación"
data={data.precipitation}
unit="mm"
chartOptions={precipitationChartOptions}
chartConfig={chartConfig}
color="rgba(26, 51, 237, 1)"
isChartLoading={isChartLoading}
startAtZero
/>
<WeatherChart
title="Temperatura Máxima"
data={data.tempMax}
unit="°C"
chartOptions={chartOptions}
chartConfig={chartConfig}
color="rgba(163, 36, 36, 1)"
isChartLoading={isChartLoading}
/>
Expand All @@ -386,17 +345,13 @@ const Dashboard = () => {
title="Temperatura Mínima"
data={data.tempMin}
unit="°C"
chartOptions={chartOptions}
chartConfig={chartConfig}
color="rgba(184, 84, 13, 1)"
isChartLoading={isChartLoading}
/>
<WeatherChart
title="Radiación Solar"
data={data.solRad}
unit="MJ"
chartOptions={chartOptions}
chartConfig={chartConfig}
color="rgba(237, 185, 12, 1)"
isChartLoading={isChartLoading}
/>
Expand Down

0 comments on commit cd0b726

Please sign in to comment.