plotly / Plotly.NET

interactive graphing library for .NET programming languages :chart_with_upwards_trend:
https://plotly.net
MIT License
663 stars 88 forks source link

load a custom font from file (ttf, woff, ...others) #438

Closed jaguarxii closed 1 month ago

jaguarxii commented 10 months ago

Description

Currently only installed fonts are available for styling charts, it would be very helpful to load custom fonts from its source such as TTF, WOFF fonts (other formats are also welcome).

Repro steps

I created a c# project using dotnet8, plptly.net 4.2, Plotly.NET.ImageExport 5.0.1, this is the code for a basic chart:

` // Creating a bar trace Plotly.NET.Trace barTrace = new Plotly.NET.Trace("bar");

        barTrace.SetValue("name", "cantidad n");
        barTrace.SetValue("x", xValues);
        barTrace.SetValue("y", yValues);

        // Creating a layout
        LinearAxis xAxis = new LinearAxis();
        xAxis.SetValue("title", "xAxis");
        xAxis.SetValue("zerolinecolor", "#ffff");
        xAxis.SetValue("gridcolor", "#ffff");
        xAxis.SetValue("showline", true);
        xAxis.SetValue("zerolinewidth", 2);

        LinearAxis yAxis = new LinearAxis();
        yAxis.SetValue("title", "yAxis");
        yAxis.SetValue("zerolinecolor", "#ffff");
        yAxis.SetValue("gridcolor", "#ffff");
        yAxis.SetValue("showline", true);
        yAxis.SetValue("zerolinewidth", 2);

        Layout layout = new Layout();
        layout.SetValue("xaxis", xAxis);
        layout.SetValue("yaxis", yAxis);
        layout.SetValue("title", "A Figure Specified by DynamicObj");
        layout.SetValue("paper_bgcolor", "#00000000");
        layout.SetValue("plot_bgcolor", "#00000000");
        //layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("Poppins")));
        //layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("DejaVu Serif")));
        layout.SetValue("showlegend", true);

        Config config = new Config();
        config.SetValue("plot_bgcolor", "#00000000");

        GenericChart
            .ofTraceObject(true, barTrace)
            .WithLayout(layout)
            .WithConfig(config)
            .ToSVGString()

`

. in a windows environment there is no trouble to get the chart with its labels . In a linux environment there are no labels, instead empty squares image

Expected behavior

It would be very useful to load an arbitrary font from its source file, similar to a Font-face setting in a web page.

for instance: layout.SetValue("font", Font.init(Family:FontFamily.NewCustom("DejaVu Serif", "font_file_path")))

Related information

kMutagene commented 9 months ago

You should be able to do this as long as there is a way to load the font in the html enclosing the chart.

A quick search for me turned out that this must be done via css (https://stackoverflow.com/questions/64508137/how-to-use-ttf-as-font-family-in-html). You can include such a css file in your chart output head tags by usingh the DisplayOptions API just as it is done here for the bulma stylesheet.

jaguarxii commented 9 months ago

Hi, in my use case there is no web page for the chart, I need the svg-rendered chart so it can be embedded into a pdf file (server-side).

it is really strange because using a proper linux distro (debian 12) there is no font for the charts (squares instead of letters), using a wsl instance (debian 11) i got proper letters (dejavusansmono)

image

somehow it takes a "default font" from the wsl instance eventhough x-server is not installed.

kMutagene commented 9 months ago

Hi, in my use case there is no web page for the chart, I need the svg-rendered chart so it can be embedded into a pdf file (server-side).

You will still need the html page to display properly, because rendering as svg is done in a headless browser. So i would expect everything to work once you manage the chart to reference the font via css

kMutagene commented 9 months ago

could you maybe attach the svg that has no font as well? maybe that helps narrowing it down

jaguarxii commented 9 months ago

Here is the sample SVG

it seems a valid file, but when it is attached to the PDF it has wrong letters (font)

image

out_svg

kMutagene commented 9 months ago

it seems a valid file, but when it is attached to the PDF it has wrong letters (font)

Are you creating that pdf programmatically? You can include fonts in pdfs, so that might be a solution for you.

I did some digging and this is not natively supported in plotly.js either (see here for reference). This means you have to either patch the generated SVG or the PDF with your custom font if you want to make the files self contained.

jaguarxii commented 9 months ago

Hi @kMutagene, thanks for the references, will test and come back with the results

The PDF file is created programmatically using QuestPDF (already created an issue about this behavior), it includes the fonts that are required. QuestPDF Issue

Using Plotly.js (client side) I had no troubles creating PDFs using pdfmake, the approach was the same, render the chart in SVG format then embed in the PDF document.

kMutagene commented 9 months ago

Using Plotly.js (client side) I had no troubles creating PDFs using pdfmake, the approach was the same, render the chart in SVG format then embed in the PDF document.

Could you maybe also attach that pdf that is working here? maybe we can spot a difference between the 2

sproott commented 9 months ago

As I've mentioned in the linked QuestPDF issue, I have the same issue with QuestPDF and ScottPlot 5, so it might be an issue on the QuestPDF side.

kMutagene commented 1 month ago

closed as this seems to be an external issue