🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

astro-charts

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astro-charts

A server-side-rendered charting library for Astro.

0.1.0
latest
Source
npm
Version published
Weekly downloads
2
-50%
Maintainers
0
Weekly downloads
 
Created
Source

astro-charts

A server-side-rendered charting library for Astro. Based on fresh_charts.

Installation

npm install astro-charts

Usage

[SSR] Inline chart example

This provides a chart rendered within the page itself.

---
import { Chart } from "astro-charts/components";
import { ChartColors, transparentize } from "astro-charts/utils";
---

<html lang="en">
  <head>
    <title>Example Chart</title>
  </head>
  <body>
    <div class="p-4 mx-auto max-w-screen-md">
      <Chart
        type="line"
        options={{
          devicePixelRatio: 1,
          scales: { y: { beginAtZero: true } },
        }}
        data={{
          labels: ["1", "2", "3"],
          datasets: [
            {
              label: "Sessions",
              data: [123, 234, 234],
              borderColor: ChartColors.Red,
              backgroundColor: transparentize(ChartColors.Red, 0.5),
              borderWidth: 1,
            },
            {
              label: "Users",
              data: [346, 233, 123],
              borderColor: ChartColors.Blue,
              backgroundColor: transparentize(ChartColors.Blue, 0.5),
              borderWidth: 1,
            },
          ],
        }}
      />
    </div>
  </body>
</html>

[SSR] Responding as an image

his page will provide the landing page of the site, which has an image link to an API route, which will send a request to /api/pages/chart.ts to render the chart.

/pages/index.astro

<html lang="en">
  <head>
    <title>Example Chart</title>
  </head>
  <body>
    <div class="p-4 mx-auto max-w-screen-md">
      <img
        src="/api/chart"
        class="mx-auto my-4 h-96"
        alt="an example chart provided as an image"
      />
    </div>
  </body>
</html>

/pages/api/chart.ts

import { renderChart } from "astro-charts";
import { ChartColors, transparentize } from "astro-charts/utils";

export function GET() {
  const cfg = { count: 7, min: -100, max: 100 };
  return renderChart({
    type: "line",
    data: {
      labels: ["1", "2", "3"],
      datasets: [
        {
          label: "Sessions",
          data: [123, 234, 234],
          borderColor: ChartColors.Red,
          backgroundColor: transparentize(ChartColors.Red, 0.5),
          borderWidth: 1,
        },
        {
          label: "Users",
          data: [346, 233, 123],
          borderColor: ChartColors.Blue,
          backgroundColor: transparentize(ChartColors.Blue, 0.5),
          borderWidth: 1,
        },
      ],
    },
    options: {
      devicePixelRatio: 1,
      scales: { y: { beginAtZero: true } },
    },
  });
}

[CSR] Inline chart example

import { ChartColors } from "astro-charts/utils";
import { Chart } from "astro-charts/react";

export default function Home() {
  return (
    <>
      <div class="p-4 mx-auto max-w-screen-md">
        <Chart
          type="line"
          options={{
            scales: { y: { beginAtZero: true } },
          }}
          data={{
            labels: ["1", "2", "3"],
            datasets: [
              {
                label: "Sessions",
                data: [123, 234, 234],
                borderColor: ChartColors.Red,
                borderWidth: 1,
              },
              {
                label: "Users",
                data: [346, 233, 123],
                borderColor: ChartColors.Blue,
                borderWidth: 1,
              },
            ],
          }}
        />
      </div>
    </>
  );
}

License

MIT

Keywords

astro-integration

FAQs

Package last updated on 21 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts