Fonts define the look of characters including letters, numbers and symbols. Many font families offer multiple font variants with different font weights or styles.
ReGraph lets you use custom fonts for text inside glyphs and labels, and it renders these custom fonts consistently across the chart.
If not set, ReGraph uses 'sans-serif' as the default font family.
See the Advanced Node Gallery with Raleway, Montserrat and Valera Round fonts.
Adding fonts
ReGraph lets you use fonts from a variety of sources including:
- Using content delivery network (CDN) link
- Using a package manager to download a package
- Hosting downloaded files (eg. CSS or
.woffor.ttffiles)
For any source, we recommend using document.fonts from the CSS Font Loading API to ensure fonts are fully loaded before rendering. This approach is also used below.
First, add the font to your HTML file in your project's root folder. We're using the Google Raleway font and a CDN link from Google Fonts to generate a script for two different font weights, 400 and 800:
For the "Hello World" app, add the script in my-regraph-app/index.html:
<!-- index.html -->
<!-- weights 400 and 800 -->
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css2?family=Raleway:wght@400;800&display=swap" />To use multiple font weights or styles of the same font, you must update your application's CSS with a unique @font-face declaration for each variant. If you are only using a single font weight or style, no CSS changes are needed.
If you are using a CDN link, click the link generated in the script to find the @font-face rules.
For the "Hello World" app, you can add the CSS code in my-regraph-app/src/App.css:
/* src/App.css */
@font-face {
font-family: 'Raleway';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('https://fonts.gstatic.com/s/raleway/v28/1Ptug8zYS_SKggPNyCMIT5lu.woff2') format('woff2');
}
@font-face {
font-family: 'Raleway Bold'; /* create a unique name for this font weight */
font-style: normal;
font-weight: 800;
font-display: swap;
src: url('https://fonts.gstatic.com/s/raleway/v28/1Ptug8zYS_SKggPNyCMIT4ttDfA.woff2') format('woff2');
}Import the React library in the file where your ReGraph component is declared. For the "Hello World" app, this is in my-regraph-app/src/App.jsx:
// src/App.jsx
import React from "react"; // will be needed laterNext, set the default fontFamily in chart options. This font is used inside labels and glyphs unless a font family is explicitly set on the label/glyph in the fontFamily property.
// src/App.jsx
<Chart
// ...
options={{ labels: { fontFamily: "Raleway" } }}
/>;Next, define and load the fonts and start the app once the fonts have loaded. Add the following code after the App() function:
// src/App.jsx
// lazy-loads the chart after the font is available
const FontReadyChart = React.lazy(() =>
Promise.all([
document.fonts.load("24px 'Raleway'"),
document.fonts.load("24px 'Raleway Bold'"), // defined in CSS in step 2
]).then(() => ({
default: App,
})),
);
// main component (Demo) renders ReGraph chart
// rendering suspended until fonts are ready
function Demo() {
return (
<React.Suspense fallback="Loading fonts...">
<FontReadyChart />
</React.Suspense>
);
}
export default Demo; // replaces export default App; previously in your codeThe fonts are now loaded and we can use them in our chart. Update the items prop:
// src/App.jsx
<Chart
items={{
node1: { label: { text: "Hello World!" } },
node2: { label: { text: "Hello Bold!", fontFamily: "Raleway Bold" } },
}}
options={{ labels: { fontFamily: "Raleway" } }}
/>;At the end of this step, you should see a custom font with two different weights in your app.
Right-to-Left Text
ReGraph supports multiple languages, including right-to-left (RTL) and bidirectional (BIDI) writing systems such as Arabic and Hebrew.
When RTL characters are detected, ReGraph applies RTL auto support by default. This is a reliable approach for a large scope of cases including:
- Mixed-system texts with both LTR and RTL characters
- Texts containing punctuation, numbers, emojis or other special characters
- Strings with symbols in strict functional order such as e-mail addresses or URLs
See the Right to Left Text example to see RTL auto support in action.
If you wish to always reverse the order of the whole text, or if you have specific requirements for more complex cases, you can use forced RTL mode:
<Chart dir="rtl" />;You can also use control characters in the label text to embed a specific direction.
For more details on using control characters, see How to use Unicode controls for bidi text (W3C documentation). Note that this page references isolated control characters, which ReGraph doesn't support.