• Writing bar chart to file

    From Cecil Westerhof@21:1/5 to All on Sat Oct 21 14:30:10 2023
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Sat Oct 21 14:58:32 2023
    Try something like:

    package require img::window
    image create photo bc -data .bc
    bc write bar.png

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to [email protected] on Sat Oct 21 15:58:56 2023
    Look for pdf4tcl: https://sourceforge.net/projects/pdf4tcl/

    You can either recode the drawing code to create a PDF bargraph, or just convert the canvas to PDF.

    Note: natively, Tcl can export a canvas to a PostScript file ($canvas postscript ...). There are utilities to convert PostScript to PDF or PNG (GhostScript can do that).

    And I believe the TkImg package can convert a canvas to an image, which could then be saved as a PNG file.


    At Sat, 21 Oct 2023 14:30:10 +0200 Cecil Westerhof <[email protected]> wrote:


    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?


    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to Cecil Westerhof on Sat Oct 21 10:29:06 2023
    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 14:44:10 UTC+2:
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof



    slides.pdf
    https://blt.sourceforge.net/

    https://wiki.tcl-lang.org/page/BLT+%2D+graph+%2D+printing+postscript

    and
    https://wiki.tcl-lang.org/page/BLT


    debian 12 with blt and blt-demo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to [email protected] on Sat Oct 21 21:38:28 2023
    [email protected] writes:

    Try something like:

    package require img::window
    image create photo bc -data .bc
    bc write bar.png

    Would be the best option, but I get:
    can't find package img::window

    Have to find out how to solve it, so at the moment I use another
    option.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From greg@21:1/5 to Cecil Westerhof on Sat Oct 21 14:14:11 2023
    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 22:28:25 UTC+2:
    greg writes:

    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 14:44:10 UTC+2:
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?



    slides.pdf
    https://blt.sourceforge.net/

    https://wiki.tcl-lang.org/page/BLT+%2D+graph+%2D+printing+postscript

    and
    https://wiki.tcl-lang.org/page/BLT
    At the moment I use:
    .bc postscript configure -landscape yes -maxpect yes
    .bc postscript output ytWeek.ps

    And I convert it to png with:
    gs -dSAFER -o ytWeek.png ytWeek.ps

    Works reasonable, but there is a problem: left and right there is a transparency area of 135 pixels and above and under of 95 pixels.
    How do I get rid of this area?
    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof


    https://manpages.debian.org/testing/blt-dev/blt::graph.3tcl.en.html

    Finally, to get hardcopy of the graph, use the postscript component.
    # Print the graph into file "file.ps"

    .g postscript output file.ps -maxpect yes -decorations no

    This generates a file file.ps containing the encapsulated PostScript of the graph. The option -maxpect says to scale the plot to the size of the page. Turning off the -decorations option denotes that no borders or color backgrounds should be drawn (i.e.
    the background of the margins, legend, and plotting area will be white).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to greg on Sat Oct 21 22:17:34 2023
    greg <[email protected]> writes:

    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 14:44:10 UTC+2:
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?



    slides.pdf
    https://blt.sourceforge.net/

    https://wiki.tcl-lang.org/page/BLT+%2D+graph+%2D+printing+postscript

    and
    https://wiki.tcl-lang.org/page/BLT

    At the moment I use:
    .bc postscript configure -landscape yes -maxpect yes
    .bc postscript output ytWeek.ps

    And I convert it to png with:
    gs -dSAFER -o ytWeek.png ytWeek.ps

    Works reasonable, but there is a problem: left and right there is a transparency area of 135 pixels and above and under of 95 pixels.
    How do I get rid of this area?

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to [email protected] on Sat Oct 21 21:22:45 2023
    At Sat, 21 Oct 2023 22:17:34 +0200 Cecil Westerhof <[email protected]> wrote:


    greg <[email protected]> writes:

    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 14:44:10 UTC+2:
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?



    slides.pdf
    https://blt.sourceforge.net/

    https://wiki.tcl-lang.org/page/BLT+%2D+graph+%2D+printing+postscript

    and
    https://wiki.tcl-lang.org/page/BLT

    At the moment I use:
    .bc postscript configure -landscape yes -maxpect yes
    .bc postscript output ytWeek.ps

    And I convert it to png with:
    gs -dSAFER -o ytWeek.png ytWeek.ps

    Works reasonable, but there is a problem: left and right there is a transparency area of 135 pixels and above and under of 95 pixels.
    How do I get rid of this area?

    GhostScript is probably creating a "page" (eg 8.5" x 11") and is filling out the "empty" "paper". You probably need to set the paper size to match the image (or something like that). I wonder if ImageMagick will take care of that...

    sudo apt install imagemagick



    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Cecil Westerhof on Sun Oct 22 01:08:43 2023
    Cecil Westerhof <[email protected]> writes:

    greg <[email protected]> writes:

    Cecil Westerhof schrieb am Samstag, 21. Oktober 2023 um 14:44:10 UTC+2:
    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr} -label {}
    pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    What is a good resource for learning to work with Tk, (bar) charts and
    the like?



    slides.pdf
    https://blt.sourceforge.net/

    https://wiki.tcl-lang.org/page/BLT+%2D+graph+%2D+printing+postscript

    and
    https://wiki.tcl-lang.org/page/BLT

    At the moment I use:
    .bc postscript configure -landscape yes -maxpect yes
    .bc postscript output ytWeek.ps

    And I convert it to png with:
    gs -dSAFER -o ytWeek.png ytWeek.ps

    Works reasonable, but there is a problem: left and right there is a transparency area of 135 pixels and above and under of 95 pixels.
    How do I get rid of this area?

    By using convert from Image Magick:
    convert -trim -resize 800x ytWeek.ps ytWeek.png

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Pitcher@21:1/5 to Cecil Westerhof on Sun Oct 22 12:17:08 2023
    On Sat, 21 Oct 2023 14:30:10 +0200
    Cecil Westerhof <[email protected]> wrote:

    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr}
    -label {} pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    Yes. I tested this on Ubuntu 18 with package tk8.6-blt2.5, which gives
    BLT version 2.5.3.

    # Create a image to hold the graph, use the "snap" subcommand and then
    # write the image to a file.
    image create photo snapshot
    .bc snap snapshot
    snapshot write snapshot.png -format png



    What is a good resource for learning to work with Tk, (bar) charts and
    the like?


    Tcl.

    I didn't have your database as a data set, so the expr function can
    quickly generate data sets based upon any function you can imagine -

    # Sample graph data generator.
    proc grfunc {x} {
    set xr [expr $x * 3.14159 / 180]
    return [expr sin($xr) + 4 * cos($xr)]
    }

    set xseries ""
    set yseries ""
    set xstep 5

    for {set x 0} {$x < 360} {incr x $xstep} {
    lappend xseries $x
    lappend yseries [grfunc $x]
    }

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata $xseries -ydata $yseries \
    -label {} -barwidth $xstep
    pack .bc -side top -expand yes -fill both


    Kind regards,
    Scott


    --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Sun Oct 22 03:27:26 2023
    can't find package img::window

    you need the Img package. It can be found on sourceforge.net as tkimg

    package require Img loads all the additional image format handlers.

    package require img::window just loads the handler for reading Tk windows

    Note the capitalized "I" when loading the overall package.

    The png handler is already part of Tk
    I use the ...Linux64 version of the package, however the documentation appears to only be in the source package.

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Scott Pitcher on Sun Oct 22 16:07:30 2023
    Scott Pitcher <[email protected]> writes:

    On Sat, 21 Oct 2023 14:30:10 +0200
    Cecil Westerhof <[email protected]> wrote:

    Three years ago I played a bit with bar charts.
    I started a YouTube channel and I like to create some bar charts.
    An example is:
    #!/usr/bin/env tclsh


    package require Tk
    package require BLT

    package require sqlite3


    set viewsArr {}
    set weekArr {}

    sqlite3 db ~/Databases/youtube.sqlite
    db eval {
    SELECT strftime('%W', dayDate, '+1 day') AS WeekNo
    , SUM(dayViews) AS WeekTotals
    FROM dayViews
    WHERE WeekNo > '37'
    GROUP BY WeekNo
    ORDER BY WeekNo
    } {
    lappend viewsArr ${WeekTotals}
    lappend weekArr ${WeekNo}
    }
    db close

    blt::barchart .bc -plotbackground black
    .bc axis configure y -stepsize 100
    .bc element create best -xdata ${weekArr} -ydata ${viewsArr}
    -label {} pack .bc -side top -expand yes -fill both

    This gives a window with the bar chart. I can make a screen dump
    of-course, but is there a way to also write the bar chart to a PNG
    file?


    Yes. I tested this on Ubuntu 18 with package tk8.6-blt2.5, which gives
    BLT version 2.5.3.

    # Create a image to hold the graph, use the "snap" subcommand and then
    # write the image to a file.
    image create photo snapshot
    .bc snap snapshot
    snapshot write snapshot.png -format png

    Works likes a charm.
    Thanks.

    Now I have to learn to get the most out of it. ;-)

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Robert Heller on Sun Oct 22 17:32:17 2023
    Robert Heller <[email protected]> writes:

    At Sat, 21 Oct 2023 21:38:28 +0200 Cecil Westerhof <[email protected]> wrote:


    [email protected] writes:

    Try something like:

    package require img::window
    image create photo bc -data .bc
    bc write bar.png

    Would be the best option, but I get:
    can't find package img::window


    If you are on a Linux system, you need to do:

    sudo apt install TkImg

    to install the package.

    On Debian it is:
    libtk-img

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Cecil Westerhof on Sun Oct 22 19:11:12 2023
    Cecil Westerhof <[email protected]> writes:

    Yes. I tested this on Ubuntu 18 with package tk8.6-blt2.5, which gives
    BLT version 2.5.3.

    # Create a image to hold the graph, use the "snap" subcommand and then
    # write the image to a file.
    image create photo snapshot
    .bc snap snapshot
    snapshot write snapshot.png -format png

    Works likes a charm.
    Thanks.

    Now I have to learn to get the most out of it. ;-)

    By the way the generated PNG is a lot smaller as the one generated
    with the postscript/ghostscript way and at the same time looks a lot
    better.
    It is also less steps, so a very BIG win.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to [email protected] on Sun Oct 22 19:08:26 2023
    [email protected] writes:

    can't find package img::window

    you need the Img package. It can be found on sourceforge.net as tkimg

    package require Img loads all the additional image format handlers.

    package require img::window just loads the handler for reading Tk windows

    Note the capitalized "I" when loading the overall package.

    The png handler is already part of Tk
    I use the ...Linux64 version of the package, however the documentation appears to only be in the source package.

    I already had solved it, but I also tried your approach. Works. (But
    maybe because I solved the other one.) Funny: for me the two images
    look exactly the same, but the one done with Img is about 25% bigger
    as the one done with img::window.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)