Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions PLOT_FEATURE_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# WordMat Plot Command Feature

## Overview
This document describes the new command-driven plotting feature for WordMat that allows users to create plots using text commands like `plot(sin(x), -2π, 2π)`.

## Usage

### Basic Syntax
```
plot(expression, xmin, xmax)
plot(expression, xmin, xmax; option=value, option=value)
```

### Examples
```
plot(sin(x), -2π, 2π)
plot(x^2, -5, 5; title="Parabola", grid=true, width=600, height=400, dpi=150)
plot(cos(3*x), 0, 10; backend="gnuplot", linewidth=2)
plot(sin(x)/x, -20, 20; title="Sinc Function")
```

### How to Use
1. Type a plot command in your Word document
2. Select the plot command text
3. Click **WordMat → Plot Selection** in the ribbon, or use keyboard shortcut **Alt+W, P**
4. The plot will be generated and inserted as an image at the cursor location

### Supported Functions
- **Trigonometric:** sin, cos, tan, asin, acos, atan
- **Exponential/Logarithmic:** exp, log, ln
- **Other:** sqrt, abs
- **Constants:** pi (or π), e
- **Operators:** +, -, *, /, ^, ()

### Options
- **title**: Plot title (string)
- **grid**: Show grid lines (true/false)
- **width**: Image width in pixels (integer)
- **height**: Image height in pixels (integer)
- **dpi**: Image resolution (integer)
- **backend**: Rendering backend ("matplotlib" or "gnuplot")
- **linewidth**: Line thickness (number)

### Backends
- **Matplotlib** (default): Requires Python with matplotlib and numpy packages
- **Gnuplot**: Requires gnuplot executable

## Implementation Files

### VBA Modules Added/Modified
- **PlotCommandHandler.bas**: Core plotting functionality
- **PlotCommandTests.bas**: Test suite for validation
- **RibbonSubs.bas**: Added `Rib_PlotSelection` callback
- **ModuleSettings.bas**: Added `PlotSelection` to KeybShortcut enum
- **ModuleKeyboardShortcuts.bas**: Added keyboard shortcut handling

### Key Functions
- **PlotSelection()**: Main entry point, parses selected text and executes plot
- **ParsePlotCommand()**: Parses plot(...) syntax with security validation
- **RenderWithMatplotlib()**: Python/Matplotlib backend implementation
- **RenderWithGnuplot()**: Gnuplot backend implementation
- **InsertPlotImage()**: Inserts generated PNG into Word document

## Security
- Expression validation uses strict whitelist of allowed functions and operators
- No arbitrary code execution - only mathematical expressions are allowed
- Temporary files are cleaned up automatically (unless debug mode is enabled)

## Configuration
Default settings can be customized:
- Default backend (matplotlib/gnuplot)
- Default image dimensions (800x600)
- Default DPI (150)
- Backend executable paths
- Timeout settings

## Ribbon Integration
To add the Plot Selection button to the WordMat ribbon:

1. Open WordMat.dotm in Word
2. Go to File → Options → Customize Ribbon
3. Add a new button with:
- **Action**: Rib_PlotSelection
- **Label**: "Plot Selection"
- **Group**: WordMat → Graphs (or create new Commands group)

Alternatively, the ribbon XML can be modified to include:
```xml
<button id="plotSelection" label="Plot Selection" onAction="Rib_PlotSelection"
imageMso="ChartInsert" size="normal"/>
```

## Testing
Run tests with:
```vba
' Basic parsing and validation tests
RunPlotTests

' Interactive test with sample plot
TestPlotExample
```

## Dependencies
- **For Matplotlib backend**: Python with matplotlib, numpy packages
- **For Gnuplot backend**: Gnuplot executable in system PATH
- Both backends are optional - users can choose their preferred one

## Future Enhancements
- 3D plotting support
- Multi-axis plots
- Parametric plots
- Plot style customization
- Interactive plot editing
- Export to other formats (SVG, PDF)
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

WordMat creates a new ribbon-menu in Word with math functionality. You can do simple and advanced calculations on any math expression entered using the builtin equation editor, plot graphs and much more.

## New: Command-driven Plotting
WordMat now supports command-driven plotting with simple text commands:
```
plot(sin(x), -2π, 2π)
plot(x^2, -5, 5; title="Parabola", grid=true)
```
Select any plot(...) command in your document and click **Plot Selection** to generate and insert a plot image.

Supported backends: Python/Matplotlib and Gnuplot. See [PLOT_FEATURE_README.md](PLOT_FEATURE_README.md) for full details.

This GitHub-site is for people that wants to contribute to the project by reporting bugs, help fix bugs, translate or add new functionality.
See [CONTRIBUTING.md](https://github.com/Eduap-com/WordMat/blob/master/CONTRIBUTING.md)

Expand Down
2 changes: 2 additions & 0 deletions Windows/VBA-modules/ModuleKeyboardShortcuts.bas
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Sub ExecuteKeyboardShortcut(ShortcutVal As KeybShortcut)
InsertGradtegn
Case KeybShortcut.Open3DPLot
Plot3DGraph
Case KeybShortcut.PlotSelection
PlotSelection
Case Else
' UserFormShortcuts.Show
End Select
Expand Down
1 change: 1 addition & 0 deletions Windows/VBA-modules/ModuleSettings.bas
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Enum KeybShortcut
InsertRefToEqution
GradTegn
Open3DPLot
PlotSelection
End Enum

Public UFMSettings As UserFormSettings
Expand Down
Loading