nidi3 / graphviz-java

Use graphviz with pure java
Apache License 2.0
937 stars 107 forks source link

class attribute for SVG #173

Closed winnall closed 4 years ago

winnall commented 4 years ago

Is there any way of generating a class attribute on SVG elements? I'm using Graphviz-Java to generate SVG for use in a website and want to control the style with CSS, so class attributes are pretty important to me.

Steve

nidi3 commented 4 years ago

Currently, this is not supported, but let me think about it

winnall commented 4 years ago

Thanks for the rapid response. Where are the class attributes on elements currently generated? I could live with being able to add arbitrary classes to that.

nidi3 commented 4 years ago

I just found this: https://stackoverflow.com/questions/47146706/how-do-i-associate-svg-elements-generated-by-graphviz-to-elements-in-the-dot-sou#answer-61912397 This solves your issue, right?

winnall commented 4 years ago

Thanks. That does what I am looking for. But I don't have a dot file, I generate the diagrams in Java. Does that mean I have to modify AttributeConfigs? Or define my own class that implements Attributes?

winnall commented 4 years ago

OK, I've worked it out. I implemented a class as follows:

public class CSSAttributes implements Attributes<For> {

    @Override
    public Attributes<? super For> applyTo( MapAttributes<? super For> attrs ) {
        throw new UnsupportedOperationException( "Not supported yet." ); //To change body of generated methods, choose Tools | Templates.
    }

    static Attributes<? extends ForNode> className( String name ) {
        MapAttributes<ForNode> result = new MapAttributes<>();
        result.add( "class", name );
        return result;
    }

}

and called

node( "node" ).with(CSSAttributes.className("value"),

which does what I want.

Thanks for your help.

nidi3 commented 4 years ago

In a more ad hoc way, you could also use .with("class", "...") or .with(Attributes.attr("class","..."))

nidi3 commented 4 years ago

I think I'm going to make SingleAttributes public, so this will be possible:

class Css extends SingleAttributes<String, ForAll> {
    private Css(String key, String value) {
        super(key, value);
    }

    static Css className(String name) {
        return new Css("class", name);
    }
}